Just another dumb question Razz why do I get error on the cout part?


Code:
#include <iostream>

using namespace std;

int main() {
    int x;
    x = 59
    cout << x;
}

Edit by Tari: used a code box.
This is because you didn't put semi-colon after "x=59"
Thanks! I seem to miss to put them out all the time.
It's a general mistake, although I would assume that whatever IDE you are using would point out such minor errors, wouldn't it? (Unless you're doing these manually?)
Minor readability note, you should use [code] boxes when posting code here, as that preserves your spacing and is much easier to read. I edited your post above as an example.
You seem to be changing languages many different times. For example, your last code example is C++ and not C.
How do I get the window to stay up till I press a key? it's not cin.get();?

What happend to the code in the post above?

yes i changed language from C to C# and now C++ but I will stick with C++ I think.

Code:
#include <iostream>
using namespace std;
int main() {
string gender = "man";
int age = 23;
if(gender == "female" && age >= 20) {
cout << "You can ride tha boat!";
}
else if(gender == "man" && age >= 23) {
cout << "You can ride the boat!";
}
else {
cout << "Bring mom or dad!";
}

}


This is a program that tells you if you can ride the boat alone or if you need your parents to go with you..

Anyways the thing I want to do is change it so I can write my age in the consol but I seem to do it all wrong it guess..


Code:
#include <iostream>
using namespace std;
int main() {
string gender = "man";
string age;
cin >> age;
if(gender == "female" && age >= 20) {
cout << "You can ride tha boat!";
}
else if(gender == "man" && age >= 23) {
cout << "You can ride the boat!";
}
else {
cout << "Bring mom or dad!";
}

}


I want to print if I am a male or a female aswell but that seems complicated (to me).[/code]
Your main problem is that you're attempting to compare an integer to a string. Changing the age to an integer should be all you need. The following (should) do everything you say you want, and I've cleaned it up a bit.

Code:
#include <iostream>
using namespace std;

int main() {
    string gender = "male";
    unsigned int age;

    cin >> age;
    cout << gender << ", age" << age;
    if ((gender == "female" && age >= 20) || gender == "male" && age >= 23)
        cout << "You can ride the boat!";
    else
        cout << "Bring mom or dad!";
    return 0;
}
I'm not sure exactly how C++ handles the comparison, but in plain C, the comparisons against age you were doing would be checking the literal value of a pointer.

Because bitshifting things to stdout seems insane to me, here's a version that is valid C:

Code:
#include <stdio.h>
#include <string.h>

int main() {
    char *gender = "male";
    unsigned int age;

    scanf("%u", &age);
    printf("%s, age %u\n", gender, age);

    if ((!strcmp(gender, "female") && age >= 20) || !strcmp(gender, "male") && age >= 23)
        printf("You can ride the boat!\n");
    else
        printf("Bring mom or dad!\n");
    return 0;
}
Okay here you go:

Code:

#include <iostream>
using namespace std.
int age; //you had as string
int gender=2; //0==female, 1== male
int main() {
     cout << "enter age:" << '\n';
     cin >> age;
     while (gender!=1 or gender!=0) {
          cout << '\n' << "enter gender: (0=female, 1=male)" << '\n'
          cin >> gender;
     }
     //all your other stuff
return 0;

Thanks, it worked good to change age to int. Smile
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement