CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    16

    3 fields in one line "homework help"

    I don't expect anyone to do my homework for me but can someone give me some pointers... I would like to have 3 entry fields in one line, average all 3 entrys, and also the sum of all 3 for example

    Enter Northern Sales ______, _______, _______,
    Enter Northern Sales 100, 200, 300

    The actual problem is:
    Create a program that displays the sum of sales amounts made in each of four regions during a three month period. The program should also display the total sales made during the three months. The program should display each regions total sales for the three-month period, and the company's total sales for the three month period.

    By the way if you do read this I realized while typing this up that it doesn't need the average so I just need tips of 3 fields in one line... I have to take out the average part


    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
    //declare variables
    double north = 0.0;
    double south = 0.0;
    double east = 0.0;
    double west = 0.0;
    double average = 0.0;
    double averageNorth = 0.0;
    double averageSouth = 0.0;
    double averageWest = 0.0;
    double averageEast = 0.0;
    double norTol = 0.0;
    double souTol = 0.0;
    double easTol = 0.0;
    double wesTol = 0.0;
    
    
    //input value
    cout << "Enter Northern Regional Sales  ";
    cin >> north; 
    cout << "Enter Southern Regional Sales  ";
    cin >> south; 
    cout << "Enter Eastern Regional Sales  ";
    cin >> east; 
    cout << "Enter Western Regional Sales  ";
    cin >> west;
    
    //calculate change 
    
    averageNorth = (north/3);
    averageSouth = (south/3);
    averageWest = (west/3);
    averageEast = (east/3);
    
    
    //display output 
     
    cout <<"Northern Regional Average: " << averageNorth <<endl;
    cout <<"Northern Sales Total: " << norTol<<endl;
    cout <<"Southern Regional Average: " << averageSouth <<endl;
    cout <<"Northern Sales Total: " << souTol<<endl;
    cout <<"Eastern Regional Average: " << averageEast <<endl;
    cout <<"Eastern Sales Total: " << easTol<<endl;
    cout <<"Western Regional Average: " << averageWest <<endl;
    cout <<"Western Sales Total: " << wesTol<<endl;
    system ("pause");
    
    return 0;
    } //end of main function

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: 3 fields in one line "homework help"

    Subsequent ">>" inputs will simply ignore whitespace, regardless of whether subsequent entries are on the same line or not. Should be good enough. You'll have to deal with the commas manually, though, or remove them.

  3. #3
    Join Date
    Mar 2009
    Posts
    16

    Re: 3 fields in one line "homework help"

    Can you give me an example of where I would put them? on the "cin" or "cout" line?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: 3 fields in one line "homework help"

    Why would you try to do input on a cout line?

  5. #5
    Join Date
    Mar 2009
    Posts
    16

    Re: 3 fields in one line "homework help"

    Good point... How would I put it in the cin line...

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: 3 fields in one line "homework help"

    Just do 3 cin statements? I mean, there are ways to condense it further, but have you tried the basics?

  7. #7
    Join Date
    Mar 2009
    Posts
    16

    Re: 3 fields in one line "homework help"

    If you mean starting with baby steps? Basics? Yeah I am in Introduction to Programming, The class only meets once a week for 2 hours. I'm actually supposed to make a loop out of this thing but I'm going to turn it in as is because I don't get it. I will probably drop the class even though I would have to pay my scholarship back... I'm seriously ripping my hair out here...

  8. #8
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: 3 fields in one line "homework help"

    Here is an example of how to take multiple input from one line.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int number1=0;
        int number2=0;
        int number3=0;
    
    
        cout<<"Enter 3 numbers seperated by a space"<<endl;
    
        cin >>number1 >> number2 >> number3;    //    <---- MULTIPLE INPUT
    
        cout<<"Number1 : "<<number1<<endl;
        cout<<"Number2 : "<<number2<<endl;
        cout<<"Number3 : "<<number3<<endl;
    
        system("PAUSE");
    
        return(0);
    }
    As for removing commas. If you tell the user to seperate using whitespace and they put commas its not your fault.
    Google is your friend.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: 3 fields in one line "homework help"

    You could gracefully handle commas as well, but that's an additional level of complication which it's probably best not to worry about just yet. For now, allowing input to enter a fail state if it encounters an unexpected type (like a comma when it wants an int) is probably acceptable.
    Last edited by Lindley; March 24th, 2009 at 02:25 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured