Mini help regarding spaces on one line

Guys! Having trouble here. suppose a user enters in these numbers with spaces or maybe even commas. What function do I use to get all of the numbers from the same line even characters. Would really appreaciate it if someone helps me this.


1
2
3
4
5
6
7
8
9
10
11
12


 double number3;
 cout<<"\n\n\n\tEnter in number 3: ";



 cin>>number3; //suppose I typed in 123 32,345. How can i get all these numbers under one line. For instance, convert 123 32,345 in to 123323454?
 cout<< "\n\nNumber 3 is " << number3;
 } 

Depending on how you look at it, this 123 32,345 could be considered 4 separate items of data:
1
2
3
4
int 123 
int 32
char ','
int 345

but i don't think that sort of approach helps very much here.
Probably better to get the input as a string, using getline(cin, str) and then examine each character of the string and keep just the digits.
After that you would then need to convert the result to a numeric type. But this approach is problematic if you want to allow for anything more complicated than a simple unsigned integer.
It wouldn't be too bad if they are negative values. What I would do is check if there is a - sign in the front then set a variable for "negative" as true or 'y' or something then remove from the string(while removing the other non-digits ).
Though I guess technically if they put more than one negative you'd have to negate for each one. Then you can string stream like any other string-int conversion. Afterwards if the negative varaible is true multiply it by -1. or do value = - value;
Thanks for the response fellows :) Is there any easier way to do it though, by any chance? or another example that could be different from this.

Suppose I want the user to type only numbers and somehow they accidently typed in the letter s. What can I do in that conflict?
That you could use std::ignore and std::clear and a while loop.

The string method isn't too bad since a loop will do most of it for you.
Well, you could do some form of check, to see whether anything is left in the input buffer after getting a number, and if it is, issue a message asking the user to try again.

But even this would take several steps:
* get the number
* was there an error?
* if not, read rest of line into a string
* is the string empty or just spaces?
* if not, issue message and try again.

But really it depends on exactly what it is you want the program to do.
Last edited on
How about something like this? I was too lazy to put it into subprograms but this is just to give you an idea of how you would do this. Ask questions if you don't understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream> //cout , endl , getline , cin
#include <string> //string
#include <sstream> //string streams

int main()
{
	std::string input , formated = "";
	int value = 0;
	bool negative = false;
	
	std::cout << "Please enter a number: ";
	std::getline( std::cin , input );
	
	//here is how you would do with iterators
	//Method 1) ----        for( const auto &it : input );
	//method 2) ----        for( auto it = input.begin(); it != input.end(); ++it );
	//method 3) ----        for( std::string::iterator it = input.begin(); it != input.end(); ++it );
	//I will do with out
	
	int SIZE = input.size();
	for( int i = 0; i < SIZE; ++i )
	{
		if( input[i] == '-' ) negative = !( negative ); //negating
		else if( input[i] >= '0' && input[i] <= '9' ) formated += input[i]; //keep only the digits
		//you can use the is_digit function from the cctype library if you want #include <cctype>
		//else if( is_digit( input[i] ) ) formated += input[i]; //keep only the numbers
	}
	
	std::stringstream ss( formated );
	ss >> value;
	
	if( negative ) value = - value;
	
	std::cout << "Your formated input was: " << value << std::endl;
}
Repeat question.

http://www.cplusplus.com/forum/beginner/113140/

Do not post more than one thread for the same question.
Topic archived. No new replies allowed.