A small quiz.

This is just some testing i done so far. So i tried to test this and in code blocks i got red on "int questionone;"


#include <iostream>

using namespace std;

int main()

int questionone = 1;
int questiontwo=2;
int questionthree;

cout<<"Pitanje Jedan: Najveca planeta u suncevom sistemu?";
cout<<"1 : jupiter, 2 : Saturn, 3: Mars\n";
cin>>questionone;
cin.ignore();

if (questionone == 1){cout<<"Odlicno obavljen posao, Dobro su te ucili u skoli :) \n"};
else if(questionone == 2){cout<<"Netacno, vrati se u 3 razred osnovne :(.\n"};
else if(questionone == 3){cout<<"Mars, najveca?......BUHAHAAHAHAHAAHAHAHAH\n"};
cin.ignore();

cout<<"Pitanje dva: Ko je osvojio NBA titulu 2011 godine?\n";
cout<<"1: Miami heat, 2: Dalas Mavericks, 3: New Orleans \n";
cin>>questiontwo;
cin.ignore();

if(questiontwo == 1) {cout<<"Netacno, dzaba, moras nabit naocare na oci i vise na ekran :P \n"};
else if(questiontwo == 2){cout<<"A Bravo ti ga brate moj.Bice nesto od tebe! \n"};
else if(questiontwo == 3){cout<<"Da su imali bolju ekipu mozda bi dosli do finala hahaha, BY factor x\n"};
cin.get();


And this code really annoys me: If then, else commands.
Can else if be used mutiple times?

And please fix it, if this has something to do with arrays tell me. I need to make a card for my uncle "That card shows his name location" dont build it just
tell me what does it uses? Arrays, Functions, or those...advanced functions?
Your problem is that you didn't wrap the contents of main() in { }. Also, main() should return 0.
You can have if -else if- else if ... -else as many times as you like.
You don't need to wrap the command of if() in { } if it's just one line.

You could do this using an array, but you don't have to.
You need curly braces for a function block:
1
2
3
4
int main()
{
  //code here
}


And this code really annoys me: If then, else commands.
Can else if be used mutiple times?

Not sure what you mean exactly, but if you're lazy, you could omit the else in this particular case without changing the program behavior. However, you're probably better off with a switch, for example:

1
2
3
4
5
6
switch(questionone)
{
  case 1: cout<<"Odlicno obavljen posao, Dobro su te ucili u skoli :) \n"; break;
  case 2: cout<<"Netacno, vrati se u 3 razred osnovne :(.\n"; break;
  case 3: cout<<"Mars, najveca?......BUHAHAAHAHAHAAHAHAHAH\n"; break;
}


Edit:
Also, main() should return 0.

It automatically returns 0 when control reaches the end of main().
Last edited on
First of all, plss use the code tags when posting code.

Then the if-operations are wrong.
if (*IF THIS* ){ *DO THIS* };
needs to be (It's nicer to read if you put every thing on it's own line too):
1
2
3
4
if (*IF THIS* )
{
*DO THIS*;
}


If you have allot of if/else if/else in a row it's faster (for the computer too run it) and easier to read if you use the switch operation:
1
2
3
4
5
6
7
8
9
10
switch (questiontwo)
{
case 1:
*DO THIS 1*
break;
case 2:
*DO THIS 2*
break;
...
}

you can read this as being the same as:
1
2
3
4
5
6
7
8
9
if(questiontwo == 1)
{
*DO THIS 1*
}
if(questiontwo == 2)
{
*DO THIS 2*
}
...
Thank you all guys, thanks a lot!! But when hamsterman shown me the "{}" i was laughting and saying "How could i be so stupid to forgot that" i cannot believe, i am still laughting xD. Thanks very much.
//Try this code:

#include <iostream>
#include<conio.h>

using namespace std;

int main()
{
int questionone;
int questiontwo;
int questionthree;

cout<<"Pitanje Jedan: Najveca planeta u suncevom sistemu?\n";
cout<<"1 : jupiter\t 2 : Saturn\t 3: Mars\n";
cout<<"\nEnter Answer: ";
cin>>questionone;



if (questionone == 1)
cout<<"\nOdlicno obavljen posao, Dobro su te ucili u skoli :) \n";
else if(questionone == 2)
cout<<"\nNetacno, vrati se u 3 razred osnovne :(.\n";
else if(questionone == 3)
cout<<"\nMars, najveca?......BUHAHAAHAHAHAAHAHAHAH\n";


cout<<"\n\nPitanje dva: Ko je osvojio NBA titulu 2011 godine?\n";
cout<<"1: Miami heat\t 2: Dallas Mavericks\t 3: New Orleans \n";
cout<<"\nEnter Answer: ";
cin>>questiontwo;

switch(questiontwo)
{
case 1:
cout<<"\nNetacno, dzaba, moras nabit naocare na oci i vise na ekran :P \n";getch();break;
case 2:
cout<<"\nA Bravo ti ga brate moj.Bice nesto od tebe! \n";getch();break;
case 3:
cout<<"\nDa su imali bolju ekipu mozda bi dosli do finala hahaha, BY factor x\n";getch();break;

}
getch();
}

i use switch on question two so that you can identify the difference of if-else statement and switch.

This is a simple problem... you should'nt use arrays.

thanks.

Last edited on
the problem on you code is that.:

you initialized the questionone =1 and questiontwo =2;
it is wrong;

if you put it.. it is like a counter and it will never affect the if-else statements..

thanks..
Topic archived. No new replies allowed.