# Calculator

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

## Recommended Posts

Hey all :) I am coding a calculator, using strings this time. But I ran into a problem:
#include <iostream>

using namespace std;

int main()
{
int value[2] = { 0, 0 };
char indicator = 'y';
char check[4][10] = { "multiply", "divide", "substract", "add" };
char what_do_you_want_to_do[10];

cout<<"What do you want to do ( multiply, divide, substract or add )?: ";
cin.getline( what_do_you_want_to_do, 10, '\n' );

if ( what_do_you_want_to_do == check[0] )
{
cout<<"We are going to multiply";
}

return 0;
}

Does anyone know what I am doing wrong in the if function? My program does nothing when I enter multiply :) Greetings, Vinnie.

##### Share on other sites
You can't compare char arrays like that. You are comparing the addresses in the pointers, not the data pointed to.

You could do:

if(!std::strcmp(what_do_you_want_to_do,check[0])){}

However, this would be far better written to take advantage of std::string:

#include <iostream>#include <string>using namespace std;int main(){	char check[4][10] = { "multiply", "divide", "substract", "add" };	string what_do_you_want_to_do;		cout<<"What do you want to do?: ";	getline(cin,what_do_you_want_to_do);	if ( what_do_you_want_to_do == check[0] )	{		cout<<"We are going to multiply";	}	return 0;}

HTH

##### Share on other sites
Yes, comparing char arrays like this
if ( what_do_you_want_to_do == check[0] )
will compare the memory addresses of the arrays, not the contents.
eg. it will always be false.

##### Share on other sites
Quote:
 Original post by EasilyConfusedYou can't compare char arrays like that. You are comparing the addresses in the pointers, not the data pointed to.You could do:if(!std::strcmp(what_do_you_want_to_do,check[0])){}However, this would be far better written to take advantage of std::string:*** Source Snippet Removed ***HTH

Modified my code :)

#include <iostream>using namespace std;int main(){	int value[2] = { 0, 0 };	char indicator = 'y';	char check[4][10] = { "multiply", "divide", "substract", "add" };	std::string what_do_you_want_to_do[10];		cout<<"What do you want to do ( multiply, divide, substract or add )?: ";	getline(cin,what_do_you_want_to_do);	if ( what_do_you_want_to_do == check[0] )	{		cout<<"We are going to multiply";	}	return 0;}

But now I get these errors:

1>------ Build started: Project: Rekenmachine, Configuration: Debug Win32 ------
1>Compiling...
1>C++ File.cpp
1>c:\documents and settings\vinnie the gamer\mijn documenten\visual studio 2005\projects\rekenmachine\rekenmachine\c++ file.cpp(13) : error C3861: 'getline': identifier not found
1>c:\documents and settings\vinnie the gamer\mijn documenten\visual studio 2005\projects\rekenmachine\rekenmachine\c++ file.cpp(15) : error C2446: '==' : no conversion from 'char *' to 'std::string *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\vinnie the gamer\mijn documenten\visual studio 2005\projects\rekenmachine\rekenmachine\c++ file.cpp(15) : error C2440: '==' : cannot convert from 'char [10]' to 'std::string [10]'
1>Build log was saved at "file://c:\Documents and Settings\Vinnie Tha Gamer\Mijn documenten\Visual Studio 2005\Projects\Rekenmachine\Rekenmachine\Debug\BuildLog.htm"
1>Rekenmachine - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

##### Share on other sites
1) #include <string>

and 2)

NOT std::string what_do_you_want_to_do[10];

but

std::string what_do_you_want_to_do;

Your first line with the [10] on the end is creating an array of 10 std::strings where you only want one. This makes "what_do_you_want_to_do" decay to a pointer to string rather than an actual string in the later function calls, resulting in all the errors.

BTW, "what_do_you_want_to_do" is a horrible name for a local variable. Why can't you just call it "response" or somthing?

##### Share on other sites
You should also change char check[4][10] to std::string check[4] = ...

##### Share on other sites
Quote:
 Original post by pulpfistYou should also change char check[4][10] to std::string check[4] = ...

I thought that was right? 4 Names which may consist of 10 characters?

##### Share on other sites
Yea but the thing is that a std::string works like an array, so making 4 of them is like having 4 "arrays". Infact, the string will grow and shrink as needed, so you dont have to worry about its size

##### Share on other sites
Quote:
Original post by Vinniee
Quote:
 Original post by pulpfistYou should also change char check[4][10] to std::string check[4] = ...

I thought that was right? 4 Names which may consist of 10 characters?

Using c style strings (ie char arrays) you do need to specify the length so "char check[4][10]" is 4 strings of length 10. With std::string objects you dont need to give a length, they handle all that for you, hence you dont need the [10] anymore.

##### Share on other sites
One more thing...

Now that you are using string objects rather than raw char arrays, you will want to use the global version of getline. It works better with string objects:
getline(cin, what_do_you_want_to_do);

##### Share on other sites

#include <iostream>#include <string>using namespace std;int main(){	int value[2] = { 0, 0 };	char indicator = 'y';	char check[][] = { "multiply", "divide", "substract", "add" };	string what_do_you_want_to_do;	while ( indicator == 'y' || indicator == 'Y' )	{		cout<<"What do you want to do ( multiply, divide, substract or add )?: ";	getline(cin,what_do_you_want_to_do);	if ( what_do_you_want_to_do == check[0] )	{		cout<<"We are going to multiply"		<< endl;		cout<<"Please give me two values to multiply ( seperate them by a space ): ";		cin>> value[0] >> value[1];		cout<<"The answer is: " << ( value[0] * value[1] );	}		if ( what_do_you_want_to_do == check[1] )	{		cout<<"We are going to divide"		<< endl;		cout<<"Please give me two values to divide ( seperate them by a space ): ";		cin>> value[0] >> value[1];		cout<<"The answer is: " << ( value[0] / value[1] );	}	if ( what_do_you_want_to_do == check[2] )	{		cout<<"We are going to substract"		<< endl;		cout<<"Please give me two values to substract ( seperate them by a space ): ";		cin>> value[0] >> value[1];		cout<<"The answer is: " << ( value[0] - value[1] );	}	if ( what_do_you_want_to_do == check[3] )	{		cout<<"We are going to add"		<< endl;		cout<<"Please give me two values to add ( seperate them by a space ): ";		cin>> value[0] >> value[1];		cout<<"The answer is: " << ( value[0] + value[1] );	}	cout<<"Do you want to do this again?: ";	cin>> indicator;	}	return 0;}

[Edited by - Vinniee on December 14, 2006 5:42:49 AM]

##### Share on other sites
Quote:
 if ( what_do_you_want_to_do == check[0] )

Here you are comparing a string with a char[]
Wheter that works or not depends if there exists a == operator that will allow a string as the left operand, and a char array as the right operand.
To tell you the truth; Im not sure.
If it does, it will work.

if ( what_do_you_want_to_do == string(check[0]) )

This however only require a == operator that takes a string as both operands, and I know that exists, so I know that will work.

##### Share on other sites
I'm pretty sure there is, but even if there wasn't it would still work since there is a std::string constructor that takes a const char *, so the compiler would generate a temporary string object for the compare if no bool operator==(const char *) const method existed.

If Vinniee still has a problem, it is not with that. After all, it is common to do:

std::string s;
//
if(s=="hello") //

which is effectively the same thing.

##### Share on other sites
Im aware of that. I didnt want to elaborate too much. It was not my point.
But thanks for clearing that up

##### Share on other sites
I think you might have misunderstood what was said about using an array of std::string instead of array of char arrays, or you just skipped to follow the advice.

The line
char check[][] = { "multiply", "divide", "substract", "add" };

string check[] = { "multiply", "divide", "substract", "add" };

In this specific case, it might not make much of a difference, but you should always prefer using std::string rather than char-arrays/pointers when dealing with strings. And learning to do so at an early level is a good thing.

##### Share on other sites
It works, but there's still one problem left :)

I'll explain this with a printscreen:

http://img99.imageshack.us/img99/1631/naamlooshn0.jpg

As you can see, it works for the first time but it goes wrong the second time :)

Here is the code:

#include <iostream>#include <string>using namespace std;int main(){	int value[2] = { 0, 0 };	char indicator = 'y';	string check[4] = { "multiply", "divide", "substract", "add" };	string what_do_you_want_to_do;	while ( indicator == 'y' || indicator == 'Y' )	{		cout<<"What do you want to do ( multiply, divide, substract or add )?: ";	getline(cin,what_do_you_want_to_do);	if ( what_do_you_want_to_do == check[0] )	{		cout<<"We are going to multiply"			<< endl;		cout<<"Please give me two values to multiply ( seperate them by a space ): ";		cin>> value[0] >> value[1];		cout<<"The answer is: " << ( value[0] * value[1] )			<< endl;	}		if ( what_do_you_want_to_do == check[1] )	{		cout<<"We are going to divide"			<< endl;		cout<<"Please give me two values to divide ( seperate them by a space ): ";		cin>> value[0] >> value[1];		cout<<"The answer is: " << ( value[0] / value[1] )			<< endl;	}	if ( what_do_you_want_to_do == check[2] )	{		cout<<"We are going to substract"			<< endl;		cout<<"Please give me two values to substract ( seperate them by a space ): ";		cin>> value[0] >> value[1];		cout<<"The answer is: " << ( value[0] - value[1] )			<< endl;	}	if ( what_do_you_want_to_do == check[3] )	{		cout<<"We are going to add"			<< endl;		cout<<"Please give me two values to add ( seperate them by a space ): ";		cin>> value[0] >> value[1];		cout<<"The answer is: " << ( value[0] + value[1] )			<< endl;	}	cout<<"Do you want to do this again?: ";	cin>> indicator;	}	return 0;}

##### Share on other sites
I think what is happening is that after you cin >> to the char, the newline gets left in the input stream, so the second call to getline just immediatley finds the newline, leaving the previous value in what_do_... unchanged.

Try putting:

cin.ignore();

after the cin >> indicator; line.

To be honest, this is why I tend to avoid the formatted input with cin. You might be better to make indicator a string, and use another getline(cin,indicator) instead of the cin >> indicator.

Obviously your while loop condition needs to change to

while(indicator=="y" || indicator=="Y") // double quotes instead of single

but that would avoid all these trailing newline issues. Up to you really.

##### Share on other sites
Quote:
 Original post by EasilyConfusedI think what is happening is that after you cin >> to the char, the newline gets left in the input stream, so the second call to getline just immediatley finds the newline, leaving the previous value in what_do_... unchanged.Try putting:cin.ignore();after the cin >> indicator; line.To be honest, this is why I tend to avoid the formatted input with cin. You might be better to make indicator a string, and use another getline(cin,indicator) instead of the cin >> indicator.Obviously your while loop condition needs to change towhile(indicator=="y" || indicator=="Y") // double quotes instead of singlebut that would avoid all these trailing newline issues. Up to you really.

AWESOME =D It works! Thanks m8 :)

What exactly does cin.ignore(); do, and what is the difference between the single and double quotes :)?

EDIT, double quotes don't work :)

##### Share on other sites
If you use double quotes as he said, you must also make indicator a string, not a char.
and fill it with getline(cin, indicator);
and if you do that, you no longer need the cin.ignore ^^
Quote:
 What exactly does cin.ignore(); do, and what is the difference between the single and double quotes :)?

'y' is a char
"y" is a char array (which in turn will be accepted as a string)

cin.ignore

We need to use cin.ignore to get rid of the last character the user wrote (enter)
cin's >> operator leave this behind, which is not what we want.
So we use ignore to get rid of it.

##### Share on other sites
Quote:
 Original post by pulpfistIf you use double quotes as he said, you must also make indicator a string, not a char.and fill it with getline(cin, indicator);edit:and if you do that, you no longer need the cin.ignore ^

Ok will do that, but what does cin.ignore() do?

##### Share on other sites
See my last post. I added some info.

##### Share on other sites
Quote:
Original post by Vinniee
Quote:
 Original post by pulpfistIf you use double quotes as he said, you must also make indicator a string, not a char.and fill it with getline(cin, indicator);edit:and if you do that, you no longer need the cin.ignore ^

Ok will do that, but what does cin.ignore() do?

#include <iostream>#include <string>using namespace std;int main(){	int value[2] = { 0, 0 };	string check[4] = { "multiply", "divide", "substract", "add" };	string what_do_you_want_to_do;	string indicator;	do	{		cout<<"What do you want to do ( multiply, divide, substract or add )?: ";		getline( cin, what_do_you_want_to_do );		if ( what_do_you_want_to_do == check[0] )		{			cout<<"We are going to multiply"				<< endl;			cout<<"Please give me two values to multiply ( seperate them by a space ): ";			cin>> value[0] >> value[1];			cout<<"The answer is: " << ( value[0] * value[1] )				<< endl;		}				if ( what_do_you_want_to_do == check[1] )		{			cout<<"We are going to divide"				<< endl;			cout<<"Please give me two values to divide ( seperate them by a space ): ";			cin>> value[0] >> value[1];			cout<<"The answer is: " << ( value[0] / value[1] )				<< endl;		}		if ( what_do_you_want_to_do == check[2] )		{			cout<<"We are going to substract"				<< endl;			cout<<"Please give me two values to substract ( seperate them by a space ): ";			cin>> value[0] >> value[1];			cout<<"The answer is: " << ( value[0] - value[1] )				<< endl;		}		if ( what_do_you_want_to_do == check[3] )		{			cout<<"We are going to add"				<< endl;			cout<<"Please give me two values to add ( seperate them by a space ): ";			cin>> value[0] >> value[1];			cout<<"The answer is: " << ( value[0] + value[1] )				<< endl;		}		cout<<"Do you want to do this again?: ";		getline(cin, indicator);	}while ( indicator == "y" || indicator == "Y");	return 0;}

But doesn't work ( he doesn't repeat the loop when I enter a y ).

##### Share on other sites
Yea I think I know why.
Probably becouse you use cin >> earlier (inside the if's)
They will leave behind the newline char (enter) as I tried to explain above.

Try using cin.ignore after those.

##### Share on other sites
Quote:
 Original post by pulpfistYea I think I know why.Probably becouse you use cin >> earlier (inside the if's)They will leave behind the newline char (enter) as I tried to explain above.Try using cin.ignore after those.

Yeah sorry m8, english isn't my first language so this is a little bit harder for me to understand :)

It works now ;)

#include <iostream>#include <string>using namespace std;int main(){	int value[2] = { 0, 0 };	string check[4] = { "multiply", "divide", "substract", "add" };	string what_do_you_want_to_do;	string indicator;	do	{		cout<<"What do you want to do ( multiply, divide, substract or add )?: ";		getline( cin, what_do_you_want_to_do );		if ( what_do_you_want_to_do == check[0] )		{			cout<<"We are going to multiply"				<< endl;			cout<<"Please give me two values to multiply ( seperate them by a space ): ";			cin>> value[0] >> value[1];			cout<<"The answer is: " << ( value[0] * value[1] )				<< endl;		}				if ( what_do_you_want_to_do == check[1] )		{			cout<<"We are going to divide"				<< endl;			cout<<"Please give me two values to divide ( seperate them by a space ): ";			cin>> value[0] >> value[1];			cout<<"The answer is: " << ( value[0] / value[1] )				<< endl;		}		if ( what_do_you_want_to_do == check[2] )		{			cout<<"We are going to substract"				<< endl;			cout<<"Please give me two values to substract ( seperate them by a space ): ";			cin>> value[0] >> value[1];			cout<<"The answer is: " << ( value[0] - value[1] )				<< endl;		}		if ( what_do_you_want_to_do == check[3] )		{			cout<<"We are going to add"				<< endl;			cout<<"Please give me two values to add ( seperate them by a space ): ";			cin>> value[0] >> value[1];			cout<<"The answer is: " << ( value[0] + value[1] )				<< endl;		}		cin.ignore();		cout<<"Do you want to do this again?: ";		getline( cin, indicator );	}while ( indicator == "y" || indicator == "Y");	return 0;}

##### Share on other sites
Thats cool.

Quote:
 Yeah sorry m8, english isn't my first language so this is a little bit harder for me to understand :)

I didnt mean it like that mate.
You are grasping these things nicely for a beginner =)

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628653
• Total Posts
2984050

• 10
• 9
• 9
• 10
• 21