Help for beginner please!

This is a discussion on Help for beginner please! within the C++ Programming forums, part of the General Programming Boards category; Can someone please show me how I can ask a user for numbers one at a time i.e.: 1 2 ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    6

    Help for beginner please!

    Can someone please show me how I can ask a user for numbers one at a time i.e.:
    1
    2
    3
    4
    and then turn that number into one number of 1234. I have been trying everything that I can think of and can't figure it out. Please help!

    Steph

    Code:
    #include<iostream.h>
    
    
    void showvalues(int[]);
    
    void main ()
    {
    	
    	int num1[5], num2[5];
    	
    		
    		cout<< "enter number" <<endl;
    		for (int count = 0; count < 5; count++)
    		cin >> num1[count];
    
    		showvalues(num1);
    		cout << endl;
    
    		
    		cout<< "enter number" <<endl;
    		for (int count1 = 0; count1 < 5; count1++)
    		cin >> num2[count1];
    		
    		showvalues(num2);
    		cout << endl;
    		
    		
    
    }
    void showvalues(int nums[])
    {
    	for(int index = 0; index < 5; index++)
    		cout <<nums[index] << "";
    	
    }
    Last edited by sflyers; 01-09-2004 at 10:15 PM.

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Show us what you've done so far (i.e. post some code).

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    You only need cin and cout. If you haven't used them then you really need to learn more. That's all I will tell you. Also, post your code (if you have any).

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    6
    Last edited by sflyers; 01-09-2004 at 10:14 PM.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by sflyers
    This is what i last had.......I know it's completely wrong though. I didn't want you to think I wanted you to write it for me just steer me in the right direction.

    Code:
    #include<iostream.h>
    
    
    
    void main (void)
    {
    	
    	int time[5], time2[5];
    	int time3;
    		cout<< "enter number" <<endl;
    	for (int count = 0; count < 5; count++)
    		cin >> time[count];
    	cout << "nums:";
    		for(count=0; count <5;count++)
    			cout<< "" <<time[count];
    		cout <<endl;
    	cout<< "enter number" <<endl;
    	
    	for (int count1 = 0; count1 < 5; count1++)
    		cin >> time2[count1];
    	cout << "nums:";
    		for(count1=0; count1 <5;count1++)
    			cout<< "" <<time2[count1];
    		cout <<endl;
    		
    		cout << "the sum of the numbers is" << endl;
    		time3 = time[count] + time2[count1];
    		cout <<time3;
    		cout << endl;
    	
    }
    #1) fix your formatting so it's easier to read
    #2) fix your code so you don't have compiler errors
    #3) where is the code to "combine" the 4 input numbers into 1 numnber?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    As I said, cin and cout, not really any need for that function:

    Code:
    #include <iostream> //Be standard, drop the .h
    
    //using namespace std; or
    //using std :: cout;
    //using std :: endl;
    //using std :: cin;
    
    int main( void )
    {
      int Num1, Num2, Num3, Num4;
      {
        //Or
        std :: cout << "Enter a number: " << std :: endl;
        std :: cin >> Num1;
        //Go through them all (before below)
        cout << "You entered: " << Num1 << Num2 << Num3 << Num4;
      }
      //cin.get(); , system("PAUSE"); Whatever you pause with
      return 0;
    }
    Or the other one:

    Code:
    std :: cout << "Enter four numbers: " << std :: endl;
    std :: cin >> Num1 >> Num2 >> Num3 >> Num4;
    std :: cout << Num1 << Num2 << Num3 << Num4;
    =/

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by SirCrono6
    As I said, cin and cout, not really any need for that function: what function?

    Code:
        cout << "You entered: " << Num1 << Num2 << Num3 << Num4;
      }
    And how does this cout command
    Originally posted by sflyers
    turn that number into one number?
    It doesn't turn anything into anything, it simply outputs the values.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    6
    Thanks anyways.....I figured out what I needed there. I have other things to worry about now.

  9. #9
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    >What function

    void showvalues(int nums[])

    As for cout, it ouputs the value to make it appear as 1 number.

    Code:
    Enter 4 numbers: 1 2 3 4
    You entered: 1234
    Code:
    Enter a number: 1
    Enter a number: 2
    Enter a number: 3
    Enter a number: 4
    You entered: 1234
    By not going like this:

    Code:
    cout << Num1 << " " << Num2 << " " << Num3 << " " << Num4;
    Which shows the numbers as:

    Code:
    Enter 4 numbers: 1 2 3 4
    You entered: 1 2 3 4
    Same with the other one. But I suppose they aren't actually 1 number, only 4 pushed together...

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  10. #10
    Registered User
    Join Date
    Jan 2004
    Posts
    6
    I did not want to just see it as one number, it needed to become one number. I needed to have the user make 2 different numbers, entering one digit at a time and be able to get the sum of the 2 formed numbers that were formed.

  11. #11
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    ok then you'd need to add another variable named total.

    Code:
    total = num1 + nim2 + nim3 + num4;
    or if you dont want the sum, and you want the number of them all put together, then this might work.

    Code:
    total = num1, num2, num3, num4;
    or it may also work like..

    Code:
    total.assign(num1, num2, num3, num4);
    for the last one, total needs to be a string.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Aalmaron
    or if you dont want the sum, and you want the number of them all put together, then this might work.

    Code:
    total = num1, num2, num3, num4;
    That will not work. Perhaps what you are looking for is something along the lines of:
    Code:
    total = 1000 * num1 + 100 * num2 + 10 * num3 + num4;

  13. #13
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    wow that would work. i didnt think of that.

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