Thread: Can anyone help me?

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

    Can anyone help me?

    The code below takes 5 seperate numbers user entered numbers and makes them into one number, 2 seperate times. It then takes the values and adds them together for the sum. My probelm is I don't want to have to specify how many numbers (like only 5). I want them to be able to enter numbers and when they enter a -1 the number ends. can anyone help?

    Code:
     #include<iostream.h>
    
     int convertNumber(int [], int);
     void showvalues(int[]);
     
     void main ()
     {
     	
         int num1[5], num2[5];
         int val1, val2;
    			
        cout<< "enter number" <<endl;
    		for (int count = 0; count < 5; count++)
             cin >> num1[count];			
    			val1 = convertNumber(num1, 5);
    			cout << val1 << endl;
    			cout << endl;
    		
    
         cout<< "enter number" <<endl;
         for (int count1 = 0; count1 < 5; count1++)
             cin >> num2[count1];
    		 val2 = convertNumber(num2, 5);
    		 cout << val2 << endl;
    		 cout << endl;
    		 
    			 
          cout << "the sum of the numbers is: " <<  val1 + val2 << endl;
    
     }
    
    
    int convertNumber(int nums[], int arySize)
    {
        int multiplier = 1;
        int val = 0;
        for (int x = arySize - 1; x >= 0; x--)
        {
            val += nums[x] * multiplier;
            multiplier *= 10;
        }
    	return val;
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Well, you can either read up on dynamic memory so that you know how to allocate memory for your array, or you can just use the vector class.

    Code:
    while(TRUE)
    {
      cout<<"Enter a number: ";
      cin >> num;
      if(num == SENTINEL) break;
      myVector.pushback(num);
    }
    I think that'll do what you want.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    195
    Or if you don't use vectors do this:

    [code]
    int input=0;
    int sum=0;

    while (input!=-1) {
    cout<<"enter your number sil vous plait"<<endl;
    cin>> input;
    sum+=input
    }

  4. #4
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    > #include <iostream.h>

    Code:
    #include <iostream>
    >

    Code:
    using namespace std;
    or

    Code:
    using std :: cout;
    using std :: cin; // etc.
    or

    Code:
    std :: cout << "..." << std :: endl;
    >void main()

    Code:
    int main()
    - SirCrono6

    P.S. I'm a standard boy =/
    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

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Standards aside, I think that the question needs more explanation, because "5 seperate numbers user entered numbers and makes them into one number, 2 seperate times" sounds like it was written by someone who was a little drunk, or was trying for a tongue twister

    Could you provide an example of input and output?

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    6
    example output;

    enter number (one at a time)
    5
    6
    7
    -1

    enter second number (one at time)

    6
    4
    3
    2
    -1

    number enter where 567 and 6432
    the sum is 6999

    What I have below is what I have now but the if statement doesn't really do what I want it to. It end the number but then it messes up the sum part. Any ideas???

    Code:
         
          
     
         
     #include<iostream.h>
    
     int convertNumber(int [], int);
     void showvalues(int[]);
     
     void main ()
     {
    	int num1[5], num2[5];
    	int val1, val2;
    			
    	cout<< "enter number" <<endl;
    	for (int count = 0; count < 5; count++)
    	{
    		cin >> num1[count];
    		if (num1[count] == -1)
    			break;
    	}
    	  
    	val1 = convertNumber(num1, 5);
    	cout << endl;
    		
    
    	cout<< "enter number" <<endl;
    	for (int count1 = 0; count1 < 5; count1++)
    	{
    		cin >> num2[count1];
    		if (num2[count1] == -1)
    			break;
    	}
    	
    	val2 = convertNumber(num2, 5);
    	cout << endl;
    	
    	cout << "You entered the numbers:" << val1 << " and " << val2 << endl;	 
    	cout << "the sum of the numbers is: " <<  val1 + val2 << endl;
    
     }
    
    
    int convertNumber(int nums[], int arySize)
    {
        int multiplier = 1;
        int val = 0;
        for (int x = arySize-1 ; x >= 0; x--)
        {
            val += nums[x] * multiplier;
            multiplier *= 10;
        }
    	return val;
    }
    Last edited by sflyers; 01-11-2004 at 04:18 PM.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by sflyers
    Code:
    int convertNumber(int nums[], int arySize)
    {
        int multiplier = 1;
        int val = 0;
        for (int x = arySize-1 ; x >= 0; x--)
        {
            val += nums[x] * multiplier;
            multiplier *= 10;
        }
    	return val;
    }
    Since the last value entered is -1, how about
    Code:
    int convertNumber(int nums[])
    {
        int val = 0;
        int j = 0;
        while (nums[j] > -1)
        {
            val = (val * 10) + nums[j];
            j++;
        }
        return val;
    }
    This way you can have a larger number if you need it (and you use long)
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed