Jump to content
  • Advertisement
Sign in to follow this  
Vinniee

Calculator

This topic is 4229 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!