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

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;
}

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.

>>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.

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.

>>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.

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.

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
1 how do i put whitespace between thousands 230000.35€ to 230 000.35€
#include <iostream>
#include <locale>
#include <iomanip>

// http://en.cppreference.com/w/cpp/locale/numpunct
struct space_separated : std::numpunct<char> 
{
    // http://en.cppreference.com/w/cpp/locale/numpunct/thousands_sep
    virtual char do_thousands_sep()   const override { return ' ' ; }  // separate with spaces

    // http://en.cppreference.com/w/cpp/locale/numpunct/grouping
    virtual std::string do_grouping() const override { return "\3"; } // in groups of 3 digits
};

int main()
{
    // http://en.cppreference.com/w/cpp/io/basic_ios/imbue
    std::cout.imbue( std::locale( std::locale( "en_GB.utf8" ), new space_separated ) ) ;

    // http://en.cppreference.com/w/cpp/io/ios_base/getloc
    const auto stdout_locale = std::cout.getloc() ;

    // http://en.cppreference.com/w/cpp/locale/use_facet
    // http://en.cppreference.com/w/cpp/locale/moneypunct
    const auto& mp_iternational = std::use_facet< std::moneypunct<char,true> >( stdout_locale ) ;
    const auto& mp_brief = std::use_facet< std::moneypunct<char> >( stdout_locale ) ;

    const double pounds = 1234567.89 ;
    const long double pence = pounds * 100 ;

    std::cout << std::fixed << std::setprecision(2) << "space separated, number: " << pounds << "\n\n"
              << "space separated, trailing currency, international: " << pounds << ' ' << mp_iternational.curr_symbol() << "\n\n"
              << "space separated, trailing currency, brief: " << pounds << mp_brief.curr_symbol() << "\n\n" 

              << "locale default, international: " << std::showbase << std::put_money( pence, true ) << "\n\n"
              << "locale default, brief: "  << std::put_money(pence) << '\n' ;
              // http://en.cppreference.com/w/cpp/io/manip/put_money
}

http://coliru.stacked-crooked.com/a/b0f84d188b075ee0

1 it is not working for me, skipped when looking for precompiled header use, isnt it any way that is more simple.

**I want a program...

**In which without using array or string we can add space between numbers...

**can anyone help me??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.