Sign in to follow this  

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.

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

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 this post


Link to post
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 this post


Link to post
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.

Use strcmp or std::string instead

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
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:

*** 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by pulpfist
You 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Vinniee
Quote:
Original post by pulpfist
You 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 this post


Link to post
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 this post


Link to post
Share on other sites
What about this :)?

#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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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" };

should read
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
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.


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by pulpfist
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);

edit:
and if you do that, you no longer need the cin.ignore ^


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Vinniee
Quote:
Original post by pulpfist
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);

edit:
and if you do that, you no longer need the cin.ignore ^


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


Made a do-while loop instead of the while loop:

#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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by pulpfist
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.


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 this post


Link to post
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 this post


Link to post
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.

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this