Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

My first C++ Program (Sorry for stealing the thread... But here's MY :) )


ghost's Avatar
0 0

#include <iostream.h>

using namespace::std;

int main() { char name[32]; char hobby[32]; char question1[15]; char question2[15]; char question3[15]; int alder;

cout <<"Hello and welcome to my storytelling game.\nPlease enter the information as it's told to." << endl; cout <<"Press [ENTER] to Continue\n"; cin.get(); cout <<"First of all, What is your name?" << endl; cin >> name; cout <<"Second of all, how old are you?" << endl; cin >> alder;

if (alder <= 16) { cout <<"Ok, you're a bit young but it's OK :)"<< endl; } else if (alder >= 16) { cout <<"Great, then you can continue without getting insulted." << endl; }

cout <<"Well, let's continue… What is your main hobby?\n"; cin >> hobby;

if (strcmp(hobby, "Computers") == 0) { cout <<"Couldn't you picked anything else than computers? Geek…\n" ; } else { cout <<"Ok, so your favorite hobby is " << hobby << " :). That's nice!\n "; }

cout <<"That's all the info about YOU i need. Now you have to answer some of my questions.\n If you don't know the correct answer. You're doomed! \n"; cout <<"Ok, when you're ready for your first question, press [ENTER] to proceed!\n"; cin.get(); cout <<"Alright. What's the name of the programming language this program is written in ?\n";

cout <<" Programcode:" ; cin >> question1;

if (strcmp(question1, "C++") == 0) { cout <<"Great, now you can continue with the second question. Press [ENTER] to proceed.\n"; cin.get(); cout <<"Ok, now for question two. What is the most common language used for medium quality games, mobile applications and web plugins?\n"; cout <<"(You can't lose on this one: "; cin >> question2;

  if (strcmp(question2, &quot;Java&quot;) == 0)
  {
     cout &lt;&lt;&quot;BINGO! Now there&#39;s just one more question for you to do, then you&#39;re ready to go!&#92;n&quot;;
     cout &lt;&lt;&quot;Press [ENTER] to proceed.&#92;n&quot;;
     cin.get();

     cout &lt;&lt;&quot;Ok, seems like you&#39;ve been waiting so long to get this crappy C++ game finished.&#92;n So now i&#39;m gonna give you, the last, and final question.&quot; &lt;&lt; endl;
     cout &lt;&lt;&quot;When you&#39;re ready, press [ENTER].&#92;n&quot;;
     cin.get();

     cout &lt;&lt;&quot;Here it goes:&#92;n How many full albums has Enter Shikari released? &#92;n&quot;;
     cout &lt;&lt;&quot;(HAAHAHA, Didn&#39;t see that coming, ey? PS. Write in letters not in numbers.)&quot;;
     cin &gt;&gt; question3;

     if (strcmp(question3, &quot;One&quot;) == 0)
     {
        cout &lt;&lt;&quot;Congratulations! You&#39;re the winner of my crappy C++ game.&#92;n You can now call yourself a proud owner of a netpotato!&quot;;
        cin.get();
     }
     else
     {
        cout &lt;&lt;&quot;Aaw =/ You were SOOO close. If you wanna play again just restart the application &#92;n because i dont really know yet how to restart things in C++ :(&quot;;
        cin.get();
     }

  }
  else
  {
     cout &lt;&lt;&quot;NO! You were so close... But hey, this is only gonna bring you:&quot; &lt;&lt; endl;
     cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
     cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
     cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
     cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
     cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
     cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
  }

} else { cout <<"Oops :( Man it's not that hard. It's C++! \n"; cout <<"Sorry man, but now you're doomed. Byebye!"; }

cin.get();

return 0;

}

*UPDATED THE CODE A BIT! :) *

What do you think about that? It's like UBER basic. And i can't even get it to run because it gets some few errors :/

Anyone know what it is? And anyone can give me some critisism for the sake that i've been studying C++ in like… 4 hours (2 days, 1-2 hours /day).

Thanks :)


ghost's Avatar
0 0

Not that i'm very experienced in c++, but that looks pretty simple :b Maybe even a bit too simple. You started out with c++, great! keep reading, and show us what you've achieved in like a month or so maybe? :happy:


ghost's Avatar
0 0

if (alder <= 16) … else if (alder >= 16)

should be

if (alder <= 16) … else if (alder > 16)


ghost's Avatar
0 0

Skod92 wrote: Not that i'm very experienced in c++, but that looks pretty simple :b Maybe even a bit too simple. You started out with c++, great! keep reading, and show us what you've achieved in like a month or so maybe? :happy:

Ok! :) Thanks for the motivation! :)


ghost's Avatar
0 0

SwartMumba wrote: if (alder <= 16) … else if (alder >= 16)

should be

if (alder <= 16) … else if (alder > 16)

Ok :) Thanks. But doesn't it work the way i wrote it too ?


ghost's Avatar
0 0

It may work but it is illogical.

edit: Next time you post code, click the check box marked "Disable Smileys in this Post."


ghost's Avatar
0 0

SwartMumba wrote: It may work but it is illogical.

edit: Next time you post code, click the check box marked "Disable Smileys in this Post."

Ok… Well, thanks for the advice. And i'll do that next time. Cu !:)


SET's Avatar

SET

Peumonoultramicroscopicsilico
0 0

I saw how u didnt know how to restart the program. since you a beginner try using a goto command(its not restrting a program but it can travel you back to later block of code or forward to a newer block. TO those who know how to code i relize the Goto command is obsalete but the fact is its still a very simple way to go back or forward in code

i dont quite remeber C++ anymore i prefer VB.NET but it might be

CRAZY:

Code here

goto CRAZY


ghost's Avatar
0 0

One more thing, shouldn't your one line of code be:

using namespace std;


ghost's Avatar
0 0

SwartMumba wrote: One more thing, shouldn't your one line of code be:

using namespace std;

Ye, maybe :/ Well, i'll correct that!:)

SET wrote: I saw how u didnt know how to restart the program. since you a beginner try using a goto command(its not restrting a program but it can travel you back to later block of code or forward to a newer block. TO those who know how to code i relize the Goto command is obsalete but the fact is its still a very simple way to go back or forward in code

i dont quite remeber C++ anymore i prefer VB.NET but it might be

CRAZY:

Code here

goto CRAZY

Ok, thx! :)


ynori7's Avatar
Future Emperor of Earth
0 0

return main; should restart your program (as opposed to return 0;)


ghost's Avatar
0 0

Grinziel wrote: [quote]SwartMumba wrote: if (alder <= 16) … else if (alder >= 16)

should be

if (alder <= 16) … else if (alder > 16)

Ok :) Thanks. But doesn't it work the way i wrote it too ?[/quote]

It should be

if(alder <= 16) … else …

;)


ghost's Avatar
0 0

Also, for string inputs, you should use getline(cin, variablename);

Like for your name input, if someone types their full name instead of just their first name (you don't specify what to do exactly) it'll just read the first name and ignore the last name because of the space between them. getline() will read the full string. :)


ghost's Avatar
0 0

Skunkfoot wrote: Also, for string inputs, you should use getline(cin, variablename);

Like for your name input, if someone types their full name instead of just their first name (you don't specify what to do exactly) it'll just read the first name and ignore the last name because of the space between them. getline() will read the full string. :)

So instead of writing :

cin >> variablename;

i should write:

getline(cin, variablename); ? :) But should i output it the same way? With the if (strcmp(varname, "string") == 0) ?

Or am i totally lost ? xD


ynori7's Avatar
Future Emperor of Earth
0 0

Grinziel wrote:

So instead of writing :

cin >> variablename;

i should write:

getline(cin, variablename); ? :) But should i output it the same way? With the if (strcmp(varname, "string") == 0) ?

Or am i totally lost ? xD

that's right. the point is that if you try to do a cin, it will only take input up until it finds a space (so if someone entered "john smith" as their name, it would only store "john" into the variable). a getline will take in the entire line.


ghost's Avatar
0 0

ynori7 wrote: [quote]Grinziel wrote:

So instead of writing :

cin >> variablename;

i should write:

getline(cin, variablename); ? :) But should i output it the same way? With the if (strcmp(varname, "string") == 0) ?

Or am i totally lost ? xD

that's right. the point is that if you try to do a cin, it will only take input up until it finds a space (so if someone entered "john smith" as their name, it would only store "john" into the variable). a getline will take in the entire line.[/quote]

ok, thx :D That actually helped me a lot :) Right now i'm on a lowclass project who should be similiar to the game Mastermind. It's hard, and now i'm stuck, but i'll guess it'll turn out in some way when i have more experience in C++ :)

Wish me GL :)

Regards, Grinziel.


ghost's Avatar
0 0

But, with your updated code, the program still doesn't run.. I've tried to compile it myself, here's what I've ran into:

markupif (strcmp(hobby, &quot;Computers&quot;;) == 0)

Why is there a ';' within the strcmp function? You should put a ';' at the end of a full sentence of C++, but not at the end of the variables in a function.

The new code:


using namespace::std;

int main()
{
char name[32];
char hobby[32];
char question1[15];
char question2[15];
char question3[15];
int alder;

cout &lt;&lt;&quot;Hello and welcome to my storytelling game.&#92;nPlease enter the information as it&#39;s told to.&quot; &lt;&lt; endl;
cout &lt;&lt;&quot;Press [ENTER] to Continue&#92;n&quot;;
cin.get();
cout &lt;&lt;&quot;First of all, What is your name?&quot; &lt;&lt; endl;
cin &gt;&gt; name;
cout &lt;&lt;&quot;Second of all, how old are you?&quot; &lt;&lt; endl;
cin &gt;&gt; alder;

if (alder &lt;= 16)
{
cout &lt;&lt;&quot;Ok, you&#39;re a bit young but it&#39;s OK :)&quot;&lt;&lt; endl;
}
else if (alder &gt;= 16)
{
cout &lt;&lt;&quot;Great, then you can continue without getting insulted.&quot; &lt;&lt; endl;
}

cout &lt;&lt;&quot;Well, let&#39;s continue... What is your main hobby?&#92;n&quot;;
cin &gt;&gt; hobby;

if (strcmp(hobby, &quot;Computers&quot;) == 0)
{
cout &lt;&lt;&quot;Couldn&#39;t you picked anything else than computers? Geek...&#92;n&quot; ;
}
else
{
cout &lt;&lt;&quot;Ok, so your favorite hobby is &quot; &lt;&lt; hobby &lt;&lt; &quot;:) . That&#39;s nice!&#92;n &quot;;
}

cout &lt;&lt;&quot;That&#39;s all the info about YOU i need. Now you have to answer some of my questions.&#92;n If you don&#39;t know the correct answer. You&#39;re doomed! &#92;n&quot;;
cout &lt;&lt;&quot;Ok, when you&#39;re ready for your first question, press [ENTER] to proceed!&#92;n&quot;;
cin.get();
cout &lt;&lt;&quot;Alright. What&#39;s the name of the programming language this program is written in ?&#92;n&quot;;

cout &lt;&lt;&quot; Programcode:&quot; ;
cin &gt;&gt; question1;

if (strcmp(question1, &quot;C++&quot;) == 0)
{
cout &lt;&lt;&quot;Great, now you can continue with the second question. Press [ENTER] to proceed.&#92;n&quot;;
cin.get();
cout &lt;&lt;&quot;Ok, now for question two. What is the most common language used for medium quality games, mobile applications and web plugins?&#92;n&quot;;
cout &lt;&lt;&quot;(You can&#39;t lose on this one: &quot;;
cin &gt;&gt; question2;

if (strcmp(question2, &quot;Java&quot;) == 0)
{
cout &lt;&lt;&quot;BINGO! Now there&#39;s just one more question for you to do, then you&#39;re ready to go!&#92;n&quot;;
cout &lt;&lt;&quot;Press [ENTER] to proceed.&#92;n&quot;;
cin.get();

cout &lt;&lt;&quot;Ok, seems like you&#39;ve been waiting so long to get this crappy C++ game finished.&#92;n So now i&#39;m gonna give you, the last, and final question.&quot; &lt;&lt; endl;
cout &lt;&lt;&quot;When you&#39;re ready, press [ENTER].&#92;n&quot;;
cin.get();

cout &lt;&lt;&quot;Here it goes:&#92;n How many full albums has Enter Shikari released? &#92;n&quot;;
cout &lt;&lt;&quot;(HAAHAHA, Didn&#39;t see that coming, ey? PS. Write in letters not in numbers.)&quot;;
cin &gt;&gt; question3;

if (strcmp(question3, &quot;One&quot;) == 0)
{
cout &lt;&lt;&quot;Congratulations! You&#39;re the winner of my crappy C++ game.&#92;n You can now call yourself a proud owner of a netpotato!&quot;;
cin.get();
}
else
{
cout &lt;&lt;&quot;Aaw =/ You were SOOO close. If you wanna play again just restart the application &#92;n because i dont really know yet how to restart things in C++ &quot;;
cin.get();
}

}
else
{
cout &lt;&lt;&quot;NO! You were so close... But hey, this is only gonna bring you:&quot; &lt;&lt; endl;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
}

}
else
{
cout &lt;&lt;&quot;Oops Man :( it&#39;s not that hard. It&#39;s C++! &#92;n&quot;;
cout &lt;&lt;&quot;Sorry man, but now you&#39;re doomed. Byebye!&quot;;
}

cin.get();

return 0;

}

Happy coding ;) -Tov-


ghost's Avatar
0 0

-Tov- wrote: But, with your updated code, the program still doesn't run.. I've tried to compile it myself, here's what I've ran into:

markupif (strcmp(hobby, &quot;Computers&quot;;) == 0)

Why is there a ';' within the strcmp function? You should put a ';' at the end of a full sentence of C++, but not at the end of the variables in a function.

The new code:


using namespace::std;

int main()

{
char name[32];
char hobby[32];
char question1[15];
char question2[15];
char question3[15];
int alder;

cout &lt;&lt;&quot;Hello and welcome to my storytelling game.&#92;nPlease enter the information as it&#39;s told to.&quot; &lt;&lt; endl;
cout &lt;&lt;&quot;Press [ENTER] to Continue&#92;n&quot;;
cin.get();
cout &lt;&lt;&quot;First of all, What is your name?&quot; &lt;&lt; endl;
cin &gt;&gt; name;
cout &lt;&lt;&quot;Second of all, how old are you?&quot; &lt;&lt; endl;
cin &gt;&gt; alder;

if (alder &lt;= 16)
{
cout &lt;&lt;&quot;Ok, you&#39;re a bit young but it&#39;s OK :)&quot;&lt;&lt; endl;
}
else if (alder &gt;= 16)
{
cout &lt;&lt;&quot;Great, then you can continue without getting insulted.&quot; &lt;&lt; endl;
}

cout &lt;&lt;&quot;Well, let&#39;s continue... What is your main hobby?&#92;n&quot;;
cin &gt;&gt; hobby;

if (strcmp(hobby, &quot;Computers&quot;) == 0)
{
cout &lt;&lt;&quot;Couldn&#39;t you picked anything else than computers? Geek...&#92;n&quot; ;
}
else
{
cout &lt;&lt;&quot;Ok, so your favorite hobby is &quot; &lt;&lt; hobby &lt;&lt; &quot;:) . That&#39;s nice!&#92;n &quot;;
}

cout &lt;&lt;&quot;That&#39;s all the info about YOU i need. Now you have to answer some of my questions.&#92;n If you don&#39;t know the correct answer. You&#39;re doomed! &#92;n&quot;;
cout &lt;&lt;&quot;Ok, when you&#39;re ready for your first question, press [ENTER] to proceed!&#92;n&quot;;
cin.get();
cout &lt;&lt;&quot;Alright. What&#39;s the name of the programming language this program is written in ?&#92;n&quot;;

cout &lt;&lt;&quot; Programcode:&quot; ;
cin &gt;&gt; question1;

if (strcmp(question1, &quot;C++&quot;) == 0)
{
cout &lt;&lt;&quot;Great, now you can continue with the second question. Press [ENTER] to proceed.&#92;n&quot;;
cin.get();
cout &lt;&lt;&quot;Ok, now for question two. What is the most common language used for medium quality games, mobile applications and web plugins?&#92;n&quot;;
cout &lt;&lt;&quot;(You can&#39;t lose on this one: &quot;;
cin &gt;&gt; question2;

if (strcmp(question2, &quot;Java&quot;) == 0)
{
cout &lt;&lt;&quot;BINGO! Now there&#39;s just one more question for you to do, then you&#39;re ready to go!&#92;n&quot;;
cout &lt;&lt;&quot;Press [ENTER] to proceed.&#92;n&quot;;
cin.get();

cout &lt;&lt;&quot;Ok, seems like you&#39;ve been waiting so long to get this crappy C++ game finished.&#92;n So now i&#39;m gonna give you, the last, and final question.&quot; &lt;&lt; endl;
cout &lt;&lt;&quot;When you&#39;re ready, press [ENTER].&#92;n&quot;;
cin.get();

cout &lt;&lt;&quot;Here it goes:&#92;n How many full albums has Enter Shikari released? &#92;n&quot;;
cout &lt;&lt;&quot;(HAAHAHA, Didn&#39;t see that coming, ey? PS. Write in letters not in numbers.)&quot;;
cin &gt;&gt; question3;

if (strcmp(question3, &quot;One&quot;) == 0)
{
cout &lt;&lt;&quot;Congratulations! You&#39;re the winner of my crappy C++ game.&#92;n You can now call yourself a proud owner of a netpotato!&quot;;
cin.get();
}
else
{
cout &lt;&lt;&quot;Aaw =/ You were SOOO close. If you wanna play again just restart the application &#92;n because i dont really know yet how to restart things in C++ &quot;;
cin.get();
}

}
else
{
cout &lt;&lt;&quot;NO! You were so close... But hey, this is only gonna bring you:&quot; &lt;&lt; endl;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
cout &lt;&lt;&quot;ZE DOOOOOOOM! &#92;n&quot;;
}

}
else
{
cout &lt;&lt;&quot;Oops Man :( it&#39;s not that hard. It&#39;s C++! &#92;n&quot;;
cout &lt;&lt;&quot;Sorry man, but now you&#39;re doomed. Byebye!&quot;;
}

cin.get();

return 0;

}

Happy coding ;) -Tov-

Oh, that was accidental :/ Thx anyways :D