Author Topic: Learning C++! :D  (Read 3682 times)



Posted this in another thread, but it's related.
It's a solar system generator, a friend of mine wrote the core naming algorithm and I'm writing the rest of it.
Ignore ridley scotts rooster. There's a Gladiator movie case next to me and it has his name on it so I added it to the prefixes out of lack of creativity in names, and I added rooster to the names for testing purposes.

I'm currently writing an atmosphere generator to be incorporated with this code. After that I think I'll render things in 2D and then add in some physics.

Quote
#include <iostream>
int main()
{
    using std::cout;
    using std::cin;

    int Monday;
    int Tuesday;
    int Wednesday;
    int Thursday;
    int Friday;
    int Saturday;
    int Sunday;
    int day;

    cout << "Please enter what day it is today: ";
    cin >> day;
    if (day == Monday)
        cout << "Today is Monday.\n";
    if (day == Tuesday)
        cout << "Today is Tuesday\n";
    if (day == Wednesday)
        cout << "Today is Wednesday\n";
    if (day == Thursday)
        cout << "Today is Thursday\n";
    if (day == Friday)
        cout << "Today is Friday\n";
    if (day == Saturday)
        cout << "Today is Saturday\n";
    if (day == Sunday)
        cout << "Today is Sunday\n";

    return 0;
}
i can compile this, but when i type in the day it wont say "Today is Monday." etc.


i can compile this, but when i type in the day it wont say "Today is Monday." etc.

You're just creating a bunch of empty integer variables named Monday, Tuesday, Wednesday, etc.

If you want to have them enter the name of the current day, try this:
Code: [Select]
#include <iostream>
int main()
{
    using std::cout;
    using std::cin;
    using std::string;

    string Monday = "monday";
    string Tuesday = "tuesday";
    string Wednesday = "wednesday";
    string Thursday = "thursday";
    string Friday = "friday";
    string Saturday = "saturday";
    string Sunday = "sunday";
    string day;

    cout << "Please enter what day it is today: ";
    cin >> day;
    if (day == Monday)
        cout << "Today is Monday.\n";
    if (day == Tuesday)
        cout << "Today is Tuesday\n";
    if (day == Wednesday)
        cout << "Today is Wednesday\n";
    if (day == Thursday)
        cout << "Today is Thursday\n";
    if (day == Friday)
        cout << "Today is Friday\n";
    if (day == Saturday)
        cout << "Today is Saturday\n";
    if (day == Sunday)
        cout << "Today is Sunday\n";

    return 0;
}

I suggest reading up on data types.

i forgot like everything from data types from this book

and it didnt say anything about "string"



im making version 2 of the JimHello program


Code: [Select]
#include <iostream>
int main()
{
    using namespace std;

    char yourAnswer;
    char yourAnswer2;
    cout << "Jim says: Hi!   Enter your answer: \n";
    cout << "\nA. Hello, what's your name?\n";
    cout << "\nB. Um... hi.\n";
    cout << "\nC. Go away.\n";
    cin >> yourAnswer;
    if (yourAnswer == 'a')
        cout << "\nJim says: My name is Jim.\n";
        cout << "\n Enter your response: \n";
        cout << "\nA. My name is INSERTNAMEHERE. Nice to meet you!\n";
        cout << "B. Neat. Bye!\n";
        cout << "C. Don't even talk to me anymore, you're getting annoying.\n";
        cin >> yourAnswer2;
    if (yourAnswer2 == 'a')
        cout << "Jim says: Okay, well, bye!\n";
    if (yourAnswer2 == 'b')
        cout << "Jim says: Okay, see you!\n";
    if (yourAnswer2 == 'c')
        cout << "Jim says: *gasp* How rude!\n";
    if (yourAnswer == 'b')
        cout << "\nJim says: Hello.\n";
        cout << "Enter your response: \n";
        cout << "A. Bye!\n";
        cout << "B. Um, bye.\n";
        cout << "C. Leave. Now\n";
        cin >> yourAnswer2;
    if (yourAnswer == 'c')
        cout << "\nJim says: Um... okay, wasn't expecting that.\n";
        cout << "Enter your response\n";
        cout << "A. Of course you weren't.\n";
        cout << "B. I'm sorry.\n";
        cout << "C. I don't care.\n";
        cin >> yourAnswer2;
    int x;
    std::cin >>x;
    return 0;
}
doesnt work right



The first if uses multiple statements after it, so you need to add brackets around them.

Also, you should probably put all of this into a separate function from main if you know how to.

Oh, and you might want to use switches instead of stacking these ifs everywhere.

And you need something for if they don't input the correct things.

Also, what's happening is that your code is running down into the next ifs. Put a return 0 into the ifs after the yourAnswer2 things (and add brackets to the ifs)
« Last Edit: September 05, 2011, 05:05:00 AM by ECrownofFire »

Also, what's happening is that your code is running down into the next ifs. Put a return 0 into the ifs after the yourAnswer2 things (and add brackets to the ifs)
Just remember that switches can't do all types (only primitives IIRC), and that you have to use break; after each case if you don't want them to "fall through".


OP, to make your programs easier, just add a line with:
using namespace std;
So you don't have to put std:: before the cout commands and stuff.