1,105,556 Community Members

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

Member Avatar
stefy14
Newbie Poster
3 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
gabryprof
Newbie Poster
14 posts since Nov 2009
Reputation Points: 2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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;
}
Member Avatar
dusktreader
Posting Whiz in Training
255 posts since Jan 2010
Reputation Points: 137 [?]
Q&As Helped to Solve: 42 [?]
Skill Endorsements: 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.

Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
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.

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
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.

Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
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.

Member Avatar
Fbody
Posting Maven
2,845 posts since Oct 2009
Reputation Points: 682 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 7 [?]
Featured
 
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.

Member Avatar
stefy14
Newbie Poster
3 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: