here is my script:

#include <iostream>
#include <cmath>

int main()
{
using namespace std;

int who;



cout << "how are you today? good, bad, or eh?" << endl;
cin >> who;
if (who == "good")
   {
cout << "ahh thats pretty cool i guess..." << endl;
        }
if (who == "bad")
{
cout << "LOL that sucks for you!!!!" << endl;
        }
if (who == "eh")
{
cout << "EHHHHHHHHHHHHH!!!" << endl;
        }
else
{
    cout << "i dont know what that means? close and retry please..." << endl;
    }
cout << "god im so bored" << endl;
cout << endl;
system("pause")
return 0;
    
    }

i get an error :
ISO C++ forbids comparision between pointer and integer

how do i fix this? and what is actually causing this and why is it like that?


also im just starting out in codeing and wanted to know if there was a way i can make it so i can run it in anything besides CMD.
like if i could make it so when someone clicks on it , it opens up in like a window that i choose or something then it has buttons where it has the different types of code i put in there...

any help will be appreciated.
thanks.

Recommended Answers

All 51 Replies

What is the data type of your input variable?

Are you comparing it to a similar type value? No.

If you want to compare to "good" or "bad" or "crappy" or ... you must take your input as a string. If you use C-style (char array based) strings, you must the use one of the comparison functions. If you use the C++ string class, then your comparison method will work.

Master the fundamentals of console applications first, then you can move on to writing windowed programs. Or fire up Visual Basic.

Also, in the future please post your program using the code tags:

[code]

your code goes here

[/code]

What is the data type of your input variable?

Are you comparing it to a similar type value? No.

If you want to compare to "good" or "bad" or "crappy" or ... you must take your input as a string. If you use C-style (char array based) strings, you must the use one of the comparison functions. If you use the C++ string class, then your comparison method will work.

Master the fundamentals of console applications first, then you can move on to writing windowed programs. Or fire up Visual Basic.

Also, in the future please post your program using the code tags:

[code]

your code goes here

[/code]

so if i do like...

char array[3];
array[0] = good;
array[1] = bad;
array[2] = crappy;
char who;

cout << "whatever" << endl;
cin >> who

if (who == [0])
{
cout << "whatever i want" << endl;
}


if (who == [1])
{
cout << "what i want" << end;
}


if (who == [2])
{
cout << "whatever i want" << endl;
}
else
{
cout << "whatever" << endl;
}

If you are taking a class or working from a textbook on your own, I think you need to go back and review the first few chapters.

Or take some time to peruse this tutorial site.

If you are taking a class or working from a textbook on your own, I think you need to go back and review the first few chapters.

Or take some time to peruse this tutorial site.

well the thing is i know things here and there from that site from looking at things on youtube so i just wouldnt know where to start..

and no im not taking a class, i just came across c++ one day (yesterday) and decided to try it out.

Vmanes is right, but also find a good book or resource online (I know that Schildt's book is available on Microsoft's site for free -- I think you need a login/hotmail to get access to it) or look at the sticky thread at the beginning of this forum.
It's important to follow someone else's framework initially so you can get on the right track. Like any other language, you can pick up a lot in a short period of time but to do anything useful with it you must practice the correct syntax and grammar.
So kudos to you for taking it up only yesterday but be sure to get the fundamentals down early.

i already got the

int whatever

then make a few commands with it but i cant do too much yet lol
like just a second ago following a link from above i made this:

#import <iostream>
#import <cmath>

using namespace std;

int main()
{

char question[] = "please type in your mood: ";
char responding[] = "thats alot of fun to be ";
char yourname [80];
char hehe [] = "whatever... i think its fun...";
char lol [80];
char hi [] = "whatsup? pick one: nothing, everything, tired, lame";
char rofl [80];
cout << question;
cin >> yourname;
cout << responding << yourname << " isnt it?" << endl;
cin >> lol;
cout << hehe << endl;
cout << hi << endl;
cin >> rofl;
if (rofl == "nothing")
{
         cout << "you better get to do something soon then!" << endl;
         }
if (rofl == "everything")
{
         cout << "you better start doing less stuff then..." << endl;
         }
if (rofl == "tired")
{
         cout << "THEN GET SOME SLEEP!" << endl;
         }
if (rofl == "lame")
{
         cout << "this isnt lame this is fun!"<< endl;
         }
else
{
    cout << "restart and type in a real answer..." << endl;
    }

   system("pause");
   return 0;
    }

everything works the way i want it to except when it gets to the "whatsup" part and i type in what i set as an "if" function it only goes to my "else" function...
got any ideas?

but other than that do you think thats good for a first day?
im just trying to learn and ive just been messing around alittle after i posted that...

my main problem is i dont know what the difference is from

int name

and

char name

Not bad for a first day. One thing you need to learn right away is this, though.

ya i got most of the add spaces between each bit of code already but just found out about the

if (blank == "whatever") cout << "whatever" << endl;

thanks every bit helps =)

Chars are actually 1 byte integers! Check out this table. If you have a char 'a' (note the single quotes for chars) it's actually stored as (the bits representing) 97. There's lots of great resources on ASCII, Unicode (encompasses a lot more characters, including international alphabets), etc. out there on the net. Just for you own personal interest, no need to learn everything about them before you start.

well i thought since the int wasnt working i had to move to do something else then i saw the char being used and tried that and i guess is worked but in a weird way...
right now my problem is whenever i run my code it goes fine until it gets to the "whatsup"
question then no matter what i do it always goes to my "else" function instead of my "if" function...

P.S. There's some guy on that gidnetwork site named WaltP that wrote those great articles. Good thing he's not related to DaniWeb's own WaltP. That would be an eerie coincidence.

Yeah, when you have character arrays you can't compare them with the == which only works for objects of type std::string (you can bite the bullet and look at this unless you think you will get confused).
To do what you want with the char arrays, you need to look at the string portion of the C library http://www.cplusplus.com/reference/clibrary/cstring/ . Specifically look at the strcmp function.

ok ya i didnt understand that.. =)
i just got visual basic and i figured out how to make buttons and all that but how do i make it so when someone clicks on the button something pops up and its my code in a window i create?

Well, if you're going to spend some time with the language you'll have to immerse yourself in its finer points at some juncture.

In terms of windowing, for C++, you're probably a long way off from doing that properly (beyond the cookbook type approach). Visual Basic is really a whole different animal. While VB.NET and a different variant of C++, C++/CLI (with .NET), do compile to the same Intermediate Language the syntax and style and overall type-safe philosophy is different.
If you want the Winforms type approach and a language more like C++ syntax then I would grab VC# instead.

so right now im using dev C++
and everytime i run it it just runs it from CMD...
so if i were to get VC#
then it would be in the window of my choosing basically?
or how does that work?

I haven't used DevC++ in a good many years. I thought it had an Integrated Development Environment (IDE). Do you mean that you are running g++ from the command line? Btw, if you are going to stick with the g++ route (which it is an excellent compiler). I would get the Code::Blocks IDE that includes mingw (the port of Gnu Compiler Collection for windows). Dev is kind of outdated and comes with an older version of that same compiler.

All the visual studio express (the free ones) products each run in their own IDE and compile from within that environs. They do also come with command line tools if you want to go that route.

uhh honestly i had no idea what you just said o.O

No worries.

Yes, VC# runs in its own window. I was asking if you had the version of Dev-C++ with the IDE (the windows). There's the link to Code::Blocks. Take a look at youtr leisure. It would replace the Dev-C++ completely as it includes its own compiler. If what you have is working for you for the time being don't worry about using it yet.
The IDE is a place where you can do your text editing/compiling/and usually debugging all in one spot.

ok so i looked at the site you put the code::blocks one and i didnt get what to do there...

Go to (on the left side) Downloads then Binary and get codeblocks-8.02mingw-setup.exe. That package comes with the compiler and debugger along with the IDE.

ok so do you have any ideas for me to learn some of the stuff? like i tried just messing around with the stuff i thought i knew but i guess i didnt because this didnt work fully for me...

#import <iostream>
#import <cmath>

using namespace std;

int main()
{

char question[] = "please type in your mood: ";
char responding[] = "thats alot of fun to be ";
char yourname [80];
char hehe [] = "whatever... i think its fun...";
char lol [80];
char hi [] = "whatsup? pick one: nothing, everything, tired, lame";
char rofl [80];
cout << question;
cin >> yourname;
cout << responding << yourname << " isnt it?" << endl;
cin >> lol;
cout << hehe << endl;
cout << hi << endl;
cin >> rofl;
if (rofl == "nothing") cout << "you better get to do something soon then!"<< endl;
else if (rofl =="everything")cout << "then you better start doing stuff..." << endl;
else if (rofl =="tired") cout << "THEN GET SOME SLEEP!" << endl;
else if (rofl =="lame") cout << "this isnt lame this is fun!" << endl;
else cout << "restart and type in a real asnwer..." << endl;

   system("pause");
   return 0;
    }

oh and i got that file downloaded but i have no clue how to use that one...

Yeah, when you have character arrays you can't compare them with the == which only works for objects of type std::string (you can bite the bullet and look at this unless you think you will get confused).
To do what you want with the char arrays, you need to look at the string portion of the C library http://www.cplusplus.com/reference/clibrary/cstring/ . Specifically look at the strcmp function.

We went over this already. You cannot use == with character arrays.
You have to say if(strcmp(rofl,"nothing") == 0) then the char arrays are equivalent and do that for each of the else if, etc.

Either that or

#include <string>
std::string rofl = "whatever";  //instead of std:: qualifier use "using namespace std;" or better "using std::string;"
if(rofl == "whatever") 
       //do whatever
else if (rofl == "whatnot")
      //do whatever else
else
       //etc.

well for the rofl i want the user to input the data so wouldnt that just be me making it a certain piece of information?
i still dont get the
== with character arrays though

And still try entering text with a space in the middle for lol or rofl or name.

See what I wrote on post #23, I think we crossed up. You're not going to get it all at once. Read a few tutorials, peruse a book or two, try to come up with your own examples. You're not going to get it all today or tomorrow. It's a language like any other. It has turns of phrase, tongue twisters and odd puns. :)

you mean here?

char yourname [80];
char hehe [] = "whatever... i think its fun...";
char lol [80];
char hi [] = "whatsup? pick one: nothing, everything, tired, lame";
char rofl [80];

... man what have i gotten myself into with c++ xD
i think youve probably said it but im tired..
but what are some good sites for learning this like youtube i did that got to the "if and else if statements"
but right now the most i can see myself doing is making a command prompt opening up the user inputing something, depending on what they put a reply comes out... stuff like that is all i see myself being able to do at this point...
i want to learn more but the site that the other guy put (sorry cant remember your name)
didnt really help me because that was almost all the basic stuff that i already knew how to do...

There's actually quite a bit to that site if you go to the reference section. Normally I don't like sites that bump themselves up but the info there is pretty solid. There are usually little code snippets with the references which are handy to see.

Check something like this out. Skip over the pointer stuff for the time being but it goes into quite a bit of depth.

ok ill read that over tomorrow im tired... thanks for the help, also the other thing is that the program you had me download, i dont get how to use that...
any way you can help me out there?

I actually don't use C::B on a regular basis (I don't have it installed right now). I think the installation is pretty straightforward.

The trick to using it is, it's a project based approach. There should be settings there for starting a new project and then a way to add new or existing source code files to the project you've got open. Then you just build the project. After it builds with no errors, run it (there's probably something that looks like a green play button on the toolbar). There's some decent documentation on the C::B site. Unfortunately in addition to picking up the programming you have to pick up the IDE too. So like I was saying stick with what you have for the time being.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.