# Alittle help for a beginner please...

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-23-2004
Xeavor
Alittle help for a beginner please...
Ok doing a progy in c++ to average 4 numbers. Seems simple enough. Its for school, and I'm take vb and c++ together. well anyways. I can't seem to understand how to make it loop when I want and terminate when I want. I'm sure it's simple enough, but I've failed to see how to do it. Any help would be appreciated.

Code:

``` #using <mscorlib.dll> #include <stdlib.h> #include <iostream> using namespace std; using namespace System; int main() { double a = 0; double b = 0; double c = 0; double d = 0; double Average = 0.0; String *anotherAverage = ""; Boolean Y = 1; Boolean N = 0; do {system("CLS"); Console::Write("Enter first number: ");         a = Convert:: ToDouble(Console::ReadLine()); Console::Write("Enter second number: ");         b = Convert::ToDouble(Console::ReadLine()); Console::Write("Enter third number: ");         c = Convert:: ToDouble(Console::ReadLine()); Console::Write("Enter fourth number: ");         d = Convert:: ToDouble(Console::ReadLine()); Average = a + b + c + d /4; Console::WriteLine("Average: {0}", Average.ToString("N2")); Console::Write("Do you want to get another average? (Y/N)"); anotherAverage =(Console::ReadLine()); } while (anotherAverage != 0); cin.get(); return 0; } //end of main function```

oh, and this is my first ever program written from scratch...so don't be to harsh, with practice and time I hope to become ALOT better. Thanks again.
• 09-23-2004
~Kyo~
What output does it give you anyways anything?
• 09-23-2004
JarJarBinks
you should put () around a + b + c + d
so that it looks like this

((a + b + c + d)/4)

also can i recommend looking up
cout and cin
• 09-23-2004
Xeavor
yeah, I get the desired average of the numbers entered. I just can't figure out how to make it do (Y/N) part. Y for do it again, and N for not. I can get one or the other, but not both working :(.

cin.get() tells it to pause til key press...right?...cout not so sure one though, i'll look it up.
• 09-23-2004
~Kyo~
he must know about cout and cin he used cin.get()
• 09-23-2004
~Kyo~
ok well i posted too quickly i would use a character and use getch(); its alot easier and it waits for a keyboard hit just do a Y/N awnser. BTW that needs conio.h
• 09-23-2004
JarJarBinks
oh yea i ment to say

while (anotherAverage != 0);

should be

while (anotherAverage != 'N');

and anotherAverage doesnt need to be
declared as a string for using on onyl character
a char declartion is sufficent, also for best results maybe
the while should be

while(anotherAverage != 'N' || anotherAverga != 'n')
• 09-23-2004
~Kyo~
Quote:

Originally Posted by JarJarBinks
oh yea i ment to say

while (anotherAverage != 0);

should be

while (anotherAverage != N);

same thing N is defined as 0; and that would loop forever seeing as how N NEVER changes
• 09-23-2004
Xeavor
Quote:

Originally Posted by JarJarBinks
oh yea i ment to say

while (anotherAverage != 0);

should be

while (anotherAverage != 'N');

and anotherAverage doesnt need to be
declared as a string for using on onyl character
a char declartion is sufficent, also for best results maybe
the while should be

while(anotherAverage != 'N' || anotherAverga != 'n')

I had it previously that way, and I still had my issue, so i've been messin around with it.
• 09-23-2004
JarJarBinks
why is 'N' defined as 0 ?
• 09-23-2004
~Kyo~
Just make the character lower case or upper case im sure there is a function if not its easy to write...
• 09-23-2004
~Kyo~
Boolean N = 0;
• 09-23-2004
~Kyo~
0 false or what ever but still !N == true which can also be defined as any number that != 0
• 09-23-2004
JarJarBinks
Maybe this then ?

Code:

``` while (anotherAverage != 0); {     system("CLS");     Console::Write("Enter first number: ");         a = Convert:: ToDouble(Console::ReadLine());     Console::Write("Enter second number: ");         b = Convert::ToDouble(Console::ReadLine());     Console::Write("Enter third number: ");         c = Convert:: ToDouble(Console::ReadLine());     Console::Write("Enter fourth number: ");         d = Convert:: ToDouble(Console::ReadLine());     Average = ((a + b + c + d) /4);     Console::WriteLine("Average: {0}",     Average.ToString("N2"));     Console::Write("Do you want to get another average? (Y/N)");     anotherAverage =(Console::ReadLine()); }```
get rid fo the do statment and jsut replace that stuff with this?

ahh didnt see the boolean, why would you use that...
bah anyways nvm
• 09-23-2004
Xeavor
Not to change the topic, but you guys are really helpful, wish I had come here sooner lol, maybe I wouldn't have been so stressed.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last