help???

This is a discussion on help??? within the C++ Programming forums, part of the General Programming Boards category; I'm in my second week of programming and need some help here is what i have so far #include <iostream> ...

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

    help???

    I'm in my second week of programming and need some help here is what i have so far

    #include <iostream> /*for cin and cout*/


    int main () /*function main*/
    {
    int x,x2,x3,sum,number=0,product=0,largest=0,smallest= 0; /*declaration of variables*/
    float avg;


    std::cout << "Please enter an interger" << std::endl; /*input data*/
    std::cin >> x;
    std::cin >> x2;
    std::cin >> x3;


    sum = x + x2 +x3; /*total score*/
    avg = (sum)/2; /*average score*/

    std::cout << "The number of the integers is" << number << std::endl;
    std::cout << "The sum of the integers is" << sum << std::endl;
    std::cout << "The average of the integers is " << avg << std::endl;
    std::cout << "The product of the integers is " << product << std::endl;
    std::cout << "The largest integers is " << largest << std::endl;
    std::cout << "The smallest integers is " << smallest << std::endl;
    return 0;
    }

    is there any way to have infintive input until a user enters -1???

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    First check here << !! Posting Code? Read this First !! >>

    Then search the board a little and look for while loops.

    Welcome to the borad

    Basically

    Code:
    do {
    
       // stuff you want
    
    } while ( input != -1 );
    What is C++?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    sure, it's called control loops.
    Code:
    int dummy = 0;  //this is called a flag because of how it's used
     
    while (dummy != -1) //here's the start of the loop
    {
       std::cout << "Please enter an interger" <<	std::endl; /*input data*/
    
      std::cin >> x;
      std::cin >> x2;
      std::cin >> x3;
    
    
      sum = x + x2 +x3; /*total score*/
      avg = (sum)/2; /*average score*/
    
      std::cout << "The number of the integers is" << number << std::endl;
      std::cout << "The sum of the integers is" << sum << std::endl;
      std::cout << "The average of the integers is " << avg << std::endl;
      std::cout << "The product of the integers is " << product << std::endl;
      std::cout << "The largest integers is " << largest << std::endl;
      std::cout << "The smallest integers is " << smallest << std::endl;
     
      //here's where user decides to stop or continue
      std::cout << "enter -1 to stop, anything to do it all over again" << std::endl;
      std::cin >> dummy;
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    29
    thanks for the welcome i had kinda tried that but was wondering does all input from the user have to be assigned to something like x, x1, x2, x3 or could a user enter intergers endessly without having them assigned to x,x1,x2,x3 does that make any sense?

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Well the input needs to go into a variable. So just create a flag like elad stated.

    Code:
    int flag;
    
    do {
    
        cout << "Show this again? ( -1 for no ): ";
        cin  >> flag;
    
    } while ( flag != -1 );
    You can endlessly write to flag.
    What is C++?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    29
    thanks for the help ill be bcak im sure

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    std::cout << "Please enter an interger" << std::endl; /*input data*/
    std::cin >> x;
    std::cin >> x2;
    std::cin >> x3;


    with this snippet all user input up to the first time the user enters a white space character (space, tab, enter key, etc.) will go into x if the input is valid. The program will then pause until additional data and another whitespace char is entered. That data, if valid, will be put into x2. Then the program will pause again until additional data is entered and a third whitespace char is entered. That data, if valid, will go into x3. Once x, x2, and x3 have valid data, then the program will continue on. While it is possible to do this all at once as below:
    Code:
    s is a space key in the following and E is the Enter key
     
    1s23s456E
     
    now x contains 1, x2 contains 23, and x3 contains 456. There has been only one enter key pushed.
    it is not usually done like this because the user has no of knowing that x2 and x3 exist. The programmer does, but the end user doesn't, unless they are told:

    std::cout << "enter first integer" << std::endl;
    std::cin >> x;
    std::cout << "enter second integer" << std::endl;
    std::cin >> x2;
    std::cout << "enter third integer" << std::endl;
    std::cin >> x3;

    now the enter key has been used three times, but it is absolutely clear what your intentions were.

    If you want to have user enter three integers separated by a space or some other whitespace char, tell them:

    std::cout << "enter three integers separated by a space" << std::endl;
    std::cin >> x;
    std::cin >> x2;
    std::cin >> x3;
    Last edited by elad; 09-15-2004 at 12:44 PM.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    could a user enter intergers endessly without having them assigned to x,x1,x2,x3 does that make any sense?
    You can use an array. Your variabwle will look like this: x[0], x[1], x[2]... You can set your own limit by the size of array you make. You'll need to count the number of numbers input.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    elad: Why don't you use "bool" for your flag rather than "int"? It seems like it would save a little memory, since bool can only hold 1 or 0.

    Just wondering.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> elad: Why don't you use "bool" for your flag rather than "int"?

    because the OP asked:

    >> is there any way to have infintive input until a user enters -1???

    >> It seems like it would save a little memory, since bool can only hold 1 or 0.

    the smallest addressable unit of data is the byte, and that's why a bool can't be implemented with anything less than a byte (otherwise, you couldn't instantiate a bool*).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Thanks for the clarification, Sebastiani.

    As to the OP, you can always call a menu() function from within main().. something like:

    Code:
    int main()
    {
        bool flag = 0;
    
        while (flag != 1)
        {
            menu();
        }
    
        return(0);
    }
    
    int menu()
    {
        // do stuff...
    
        if (// user says yes to some kind of "are you done" question)
        {
            flag = 1;
            return(0);
        }
    
        return(0);
    }

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    My programming teacher would've killed me if I'd handed in code like this:
    Code:
    while (dummy != -1)
    Point is: use meaningful variable names.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #13
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Oh and if you use bool, you might as well check.assign true and false instead of 1 and 0.
    What is C++?

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by Lithorien
    As to the OP, you can always call a menu() function from within main().. something like:
    Ummm, that won't work, flag is local to main, and won't get updated even if it compiled (it wouldn't because flag is unidentified in menu()). Maybe something more like:
    Code:
    int main()
    {
        bool flag = false;
    
        while (!flag)
        {
            flag = menu();
        }
    
        return(0);
    }
    
    bool menu()
    {
        // do stuff...
    
        if (// user says yes to some kind of "are you done" question)
        {
            return true;
        }
    
        return false;
    }

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>could a user enter intergers endessly without having them assigned to x,x1,x2,x3 does that make any sense?

    As DougDbug said, you can start off using an array. Then when you're confident, you can try out using dynamic arrays. And when you're pretty good with those, look up std::vector or std::list, or even just "STL containers". Very useful, those things But takes a while to work up to the level where you're ready to use them.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21