Jump to content

Recommended Posts

Do you see anything wrong with:

#include <iostream>

using namespace std;

int main() {

// Setup Basic Parameters

int number1;

int number2;

int number3;

string type;

cout<<"Calculation Type (Multiply, Add, or Subtract):";

cin>>type;

cin.ignore();

cout<<"Please enter a number: ";

cin>>number1;

cin.ignore();

cout<<"Please enter a second number: ";

cin>>number2;

cin.ignore();

if (type == "Add") {

number3 = number1 + number2;

}else if (type == "Subtract") {

number3 = number1 - number2;

}else if (type == "Multiply") {

number3 = number1 * number2;

}

cout<<"Your number added up to: "<<number3 <<"\n";

cin.get();

}

I tried char type; as well but no matter what it's throwing errors around the if/else statement.  Something like...

"error C2040: '==' : 'int' differs in levels of indirection from 'const char [9]'"

Any advice would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/142180-solved-c-string-problem/
Share on other sites

"error C2040: '==' : 'int' differs in levels of indirection from 'const char [9]'"

 

 

That means that you're trying to compare an integer to a char array....  That's not happening in your code x.x.

 

 

What compiler are you using?  Wonder what string class it uses.

 

 

Just to clarify, this code compiles fine for me with VS08:

 

 

// blah1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <string>

using namespace std;

int main(int argc, const char* argv[]) {

// Setup Basic Parameters
int number1;
int number2;
int number3;

string type;

cout << "Calculation Type (Multiply, Add, or Subtract):";

cin >> type;

cin.ignore();

cout << "Please enter a number: ";

cin >> number1;

cin.ignore();

cout << "Please enter a second number: ";

cin >> number2;

cin.ignore();

bool unknown = false;
if (type == "Add") {
	number3 = number1 + number2;
} else if (type == "Subtract") {
	number3 = number1 - number2;
} else if (type == "Multiply") {
	number3 = number1 * number2;
}
else {
	cout << "Type unknown";
	unknown = true;
}

if(!unknown) {
	cout << "Your number added up to: " << number3 << "\n";
}

cin.get();

}

 

 

 

(I added the unknown part so the app doesn't say something if it shouldn't.)

 

 

 

Yours compiles fine when I add #include <string> by the way.

 

I finally got:

#include <iostream>
#include <string>
using namespace std;

int main() {
// Setup Basic Parameters
int number1;
int number2;
int number3;
char type[10];

cout<<"Calculation Type (Add, Subtract, Multiply, or Divide):";
cin>>type;
cin.ignore();

cout<<"Please enter a number: ";
cin>>number1;
cin.ignore();

cout<<"Please enter a second number: ";
cin>>number2;
cin.ignore();

if (strcmp(type, "Add") == 0) {
	number3 = number1 + number2;
}else if (strcmp(type, "Subtract") == 0) {
	number3 = number1 - number2;
}else if (strcmp(type, "Multiply") == 0) {
	number3 = number1 * number2;
}else if (strcmp(type, "Divide") == 0) {
	number3 = number1/number2;
}else {
	number3 = '0';
}
cout<<"Your number added up to: "<<number3 <<"\n";
cin.get();
}

to work instead.  However I looked at your code..i can't get it to work using string even after I try to include <string>  Not to mention I have no idea what

 

int main(int argc, const char* argv[])

I just have gotten into C++ not too long ago. I am just bouncing around writing a couple of app's here and there while I start picking up the language.

How did you learn C++ and not known what that declaration means?

 

 

The entry point function of a program can be passed parameters.

 

Ever seen something like:

 

program param1 param2

 

?

 

In essence, argv[] would be an array of command line arguments.

 

It's not technically necessary for programs, but I always add it even if I don't use it.

 

 

 

Oh by the way, you shouldn't ever need the string header in your code since you never actually use the string class.

 

 

 

I can try to help you compile my code if you want (well by my code I mean your code modded ;p).

 

What error does the compiler give you?

Oh no, I don't "know" C++.  PHP is my main language. I am finally to the point where I am ready to branch out. I have learn a few other languages but never C++.  This is one I have wanted to know awhile. I know basics...like how the data types work (to an extent) and starting to learn control structures and functions.

 

I already had my code work and had a basic calculator..I used strcmp instead of == and that worked for now. I will build on that knowledge later on.

 

RIght now I am writing two more apps. One to go to a website and get some data and analyze it then another one to take a person's name, some other information then analyze there name for various properties.

 

Basically I am trying to learn so I am presenting myself with situations adn trying to program my way out of them so I have some practical application of the language. I learn long ago that simply reading about a language takes forever and it's must faster to just jump in and get stuck a few times to really forge knowledge of the language faster.

 

Thank you again for the help.

Yeah, I've been trying to teach my self C++ for a while.  Sometimes I'll do things in C++ that would take me 2 minutes in PHP just to try something new.

 

 

As for strcmp vs ==, the string class (well the one I used anyway.... there are a lot of them) has a function defined for the == operator (in C++ you can define method to run for an operator on a class.  Pretty handy sometimes.) whereas a char array compared to a literal string would cause problems.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.