Archived

This topic is now archived and is closed to further replies.

Oh god help ignoring spaces?

This topic is 5166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a calculator. A friend suggested i start with printing the tokens. Well what if the user typed in a space? I want it to keep going if there is a space so if they wanted to they could type"3 + 3 - 3" and it would not assign the spaces to one of the arrays. I tried multiple things but as soon as it ran into the first space it stopped. Here''s my code. P.S. Plz no comments about not using standard headers.
#include<iostream.h>
#include<stdlib.h>
char input[100];
int numdigits = 0;
int numofnumbers = 0;
char signs[100];
int numofsigns = 0;
char number[100][100];
int realnumber[100][100];
bool isDigit(int inquestion){
	if(input[inquestion] == ''0'' ||input[inquestion] == ''9'' ||input[inquestion] == ''8'' ||
		input[inquestion] == ''7'' ||input[inquestion] == ''6'' ||input[inquestion] == ''5'' ||
		input[inquestion] == ''4'' ||input[inquestion] == ''3'' ||input[inquestion] == ''2'' ||
		input[inquestion] == ''1''){
		return true;
	}
	else{
		return false;
	}
}
char getInputType(int inquestion){
	return input[inquestion];
}
void getInput(){
	cout<<"Input your problem:";
	cin>>input;
}
void convertArray(){
	for(int arrayslot = 0; arrayslot < 100; arrayslot++){
		if(isDigit(arrayslot) == true){
			number[numofnumbers][numdigits] = input[arrayslot];
			realnumber[numofnumbers][0] = atoi(number[numofnumbers]);
			numdigits++;
		}
		if(isDigit(arrayslot) != true && numdigits > 0){
			signs[numofsigns] = getInputType(arrayslot);
			numofsigns++;
			numofnumbers++;
			numdigits = 0;
		}
	}	
}
void printTokens(){
	int i;
	for(i = 0; i < numofnumbers; i++){
		cout<<realnumber[i][0]<<", "<<signs[i];
		if(signs[i] != ''/0''){
			cout<<", ";
		}
	}
	cout<<"Last token is complete."<<endl;
}
void main(){
	getInput();
	convertArray();
	printTokens();
	system("pause"); 
}
I just can''t get it. Maybe I need to print this out and carry it around trying to figure it out. Thanks guys.
Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Share this post


Link to post
Share on other sites
Yeah, cin >> only reads up to the first whitespace it encounters.

Read the input into a temporary char array using getline and then loop through it el by el taking only the desirable (non-whitespace) chars.

Wizza Wuzza?

Share this post


Link to post
Share on other sites
Why does this not work then?

void getInput(){
cout<<"Input your problem:";
cin.getline(input);
}
void convertArray(){
for(int arrayslot = 0; arrayslot < 100; arrayslot++){
if(getInputType(arrayslot) != ' '){
if(isDigit(arrayslot) == true){
number[numofnumbers][numdigits] = input[arrayslot];
realnumber[numofnumbers][0] = atoi(number[numofnumbers]);
numdigits++;
}
if(isDigit(arrayslot) != true && numdigits > 0){
signs[numofsigns] = getInputType(arrayslot);
numofsigns++;
numofnumbers++;
numdigits = 0;
}
}
}
}




Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|



[edited by - ssjcory on October 21, 2003 10:51:52 PM]

Share this post


Link to post
Share on other sites
To begin with, you''ll need to have a reference about getline etc, e.g. MSDN / C++ book.

getline(char* p1, int p2, char p3)

where p1 is array to read into
p2 is the number of characters to store
p3 is the character to store upto.

getline will read your line properly, you''ll just have to read about the parameters and apply them to your sitaution.

Wizza Wuzza?

Share this post


Link to post
Share on other sites