1.11M Members

How do you add spaces between numbers to output in C++?

 
0
 

I want to my program to prompt user to input a number with three or more integers I need the program to output a space between each numebr
example: cin>>3334;
i want the output to show 3 3 3 4 each with a space and then i also need to output the sum of these number so 3+3+3+4? how do i do this can some help me asap
thanks a lot in advanced

 
0
 
int _tmain(int argc, _TCHAR* argv[])
{
	int i = 0;
	int number;
	int resto[10];
	int sum = 0;
	cout << "input number\n";
	cin >> number;
	while (number > 0)
	{	
		resto[i] = number%10;
		sum = sum + resto[i];
		number = number/10;
		i++;
	}
	for (i = i-1; i >= 0 ; i--)
	{
		cout << resto[i] << " ";
	}
	cout <<"\n sum of digits = " << sum << "\n";
	system ("PAUSE");
	return 0;
}
 
0
 

There is actually a very simple solution to this problem. First, you are trying to do two things here:

1.  Split an input number into separate digits.
2.  Add the digits together.

First of all, I would not recommend putting your input into an int type. If you do this, you have to split each digit using modular arithmetic. While this is mathematically elegant and very pleasing aesthetically, it is not the optimal solution here. Instead, one could store the input as a string. In this case, each character can then be interpreted as a digit in and of itself and processed accordingly. Then, in one pass, one could output each digit and accumulate a running sum. The method is summarized here in pseudocode:

set integer sum = 0
get input -> store in string s
for each character c in s:
    output c
    sum = sum + integer( c )
output sum

The tricky part here is converting each character to an integer. Fortunately, this is very simple in c++: int i = c - '0' This works because numbers in the ascii table are sorted in ascending order. Furthermore, chars are actually integers, so we can perform arithmetic on them. So, any numerical character minus the zero character results in the integer that the character represents: 7 == '7' - '0' Using this method, the algorithm you are trying to build can be defined in a concise main function taking only 13 lines of code ( including separate lines for each curly brace ). Try it out if you would like, and we can see how it works for you.

 
0
 

>>I want to my program to prompt user to input a number with three or more integers

You know how to get the user input into a variable right ?

int var1 = 0 , var2 = 0, var3 = 0;
cin >> var1 >> var2 >> var3; //get 3 inputs from the user

>>I need the program to output a space between each numebr

You know how to output numbers to the screen dont you?

cout << 1; //output the number 1
cout << " "; //output the string space
cout << 2 << " "; //output the number and put a space after the number

>>then i also need to output the sum of these number so 3+3+3+4?

You know how to add the variables dont you ?

int var1 = 0, int var2 = 0, int var3 = 0; //create variables
cin >> var1 >> var2 >> var3; //get inputs

int sum = var1 + var2 + var3; //add the results

//print the sum
cout << var1 << " + " << var2 << " + " << var3 << " = " << result;

Next time just take it part by part and try the problem.

 
0
 

There is actually a very simple solution to this problem. First, you are trying to do two things here:

1.  Split an input number into separate digits.
2.  Add the digits together.

First of all, I would not recommend putting your input into an int type. If you do this, you have to split each digit using modular arithmetic. While this is mathematically elegant and very pleasing aesthetically, it is not the optimal solution here. Instead, one could store the

My guess is this is exactly what the instructor wants. It teaches a technique very important to computing.

Read a value into an integer.
Use the modulus operator % and division to split the input into separate values, an array would work best.

>>I want to my program to prompt user to input a number with three or more integers

You know how to get the user input into a variable right ?

int var1 = 0 , var2 = 0, var3 = 0;
cin >> var1 >> var2 >> var3; //get 3 inputs from the user

Won't work. Try it with an input of 321.

 
0
 

>>Won't work. Try it with an input of 321.

Actually, I was under the impression that he wanted to get 3 numbers
from the user. I guess I misread his post.

 
0
 

That is what they want. But the issue is that the input has to be contiguous. Your method only works if there is some sort of whitespace between the digits.

 
0
 

Well i figured out what to do in order to accomplish what i needed the input needed to be a string
here is the program working YEP!

#include "stdafx.h"
#include<iostream>
#include<sstream>
using namespace std;

int main()
{
        //Declear Variables
                int num0 = 0;
        int sum = 0;
        string b = "";

//display Three or more numbers section
       cout<<"Enter three or more number (no dcimals): "<<endl; 
       cin>>num0;

      if ( num0 < 0 )
      num0 = -num0;

      stringstream ss;
          ss << num0; //convert to individual numbers

          ss >> b;

          for(int i = 0; i < b.length(); ++i)
    {
        cout<<b[i]<<" ";

         }

        cout<<endl;

        //Calculate sum
        while ( num0 != 0 ) {            
        sum += num0 % 10;
        num0 /= 10;
    }

         cout<<"The sum is : "<< sum <<endl;

    system("pause");

return 0;
}

//This worked out really nice yep so thats for the help peoples loved that you are gave those speedy replies thanks again
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: