Calculator

Started by
35 comments, last by Vinniee 17 years, 4 months ago
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.
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
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
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 ==========
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?
You should also change char check[4][10] to std::string check[4] = ...
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?
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
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.

Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
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);

This topic is closed to new replies.

Advertisement