Thread: Alittle help for a beginner please...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    38

    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.

  2. #2
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    What output does it give you anyways anything?

  3. #3
    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

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    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.

  5. #5
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    he must know about cout and cin he used cin.get()

  6. #6
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    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

  7. #7
    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')

  8. #8
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    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

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    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.

  10. #10
    why is 'N' defined as 0 ?

  11. #11
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Just make the character lower case or upper case im sure there is a function if not its easy to write...

  12. #12
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Boolean N = 0;

  13. #13
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    0 false or what ever but still !N == true which can also be defined as any number that != 0

  14. #14
    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
    Last edited by JarJarBinks; 09-23-2004 at 11:14 PM.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM