Ending with a space

May 3, 2012 at 11:12pm
How would i go about completing this programing without entering a space at the end when the user inputs ?

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
36
37
38
39
40
41
42
#include <iostream> // libraries
#include <iomanip>
#include <string>

using namespace std;

int main()
{
char str[80];
int i;

cout << "enter a string . Remeber to hit space at the end"<<endl; // prompting user
cin.getline(str,80); // geting line
cout<<endl;
i =0;

while (str[i]!='\0')
{
	int cnt = 0; // number counter
	int counter=0; // char counter
	//loop until I get to the space character
	while(str[i] != ' '){
		cout << str[i];
		if (str[i]>='0' & str[i] <='9'){
		 cnt+=1;
		}
		counter+=1; i+=1;
		
	}
	if(cnt == counter)
	{
		cout << " is a nunber" << endl;
	}
	else
	{
		cout << " not a number" << endl;
	}
	i++;
	//if number counter == char counter then all my charcters are numbers
	//if not then its not a number
}
}


enter a string . Remeber to hit space at the end
324dsf 323 dsaf 31

324dsf not a number
323 is a nunber
dsaf not a number
31 is a nunber
Press any key to continue . . .





May 3, 2012 at 11:25pm
you mean the new line after user input? that is be there unless u do some window manipulation or make your own window.
May 3, 2012 at 11:32pm
1
2
3
4
while(cin.getline(buffer, 80, '\n'))
{
        // ...
}


Now user can type as much as he want every time presing enter when it finishes typing a word of characters.

Dont forget to include break statment inside while loop to stop at some time.
Topic archived. No new replies allowed.