Else if not working

Im making a program that makes it looks like your computer is downloading viruses but i got stuck around one little area. I was making a part where it says else if then it says expecting a statement:

-------------------------------------------------------------------------------

// Fake virus program.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int choice1;
int choice2;
cout<<"Hi user, today i will do something really good for your computer!" << endl;
cout<<"1. Ok" << endl;
cout<<"2. What are you doing?" << endl;
cin>>choice1;
if (choice1==2)
cout<<"Too bad for you then!";
goto start;
else if (choice1==1) //right here, on this line
goto start;
start:
cout<<"Ok then, let's start!";
return 0;

---------------------------------------------------------------------------

PS. This is on Visual C++ 2010
GOTOs are a big NONO in 2011.
Also if you're putting more than one instruction in for example an if block, then you have to use curly braces.
Also use [code] [/code] for God's sake.

1
2
3
4
5
6
7
8
if (choice1==2)
{
    cout<<"Too bad for you then!";
    goto start;
}
else
    if (choice1==1) //right here, on this line
        goto start;
Thanks
PS. how does the work? I tried using that but i couldnt figure out how
1
2
3
if (choice1==2)
cout<<"Too bad for you then!";
goto start;


checks if choice1 is equal to 2 if it is 2 executes cout and then jumps to 'start' label. If it is not equal to 2 it skips the cout and jumps to 'start' label. The program will never come to 'else if' statement. So you should do it like @Catfish said.
Last edited on
Out of curiosity Catfish, why would you scorn the use of GOTO's then use one in your sample code? :p

All that you're doing is saying "If choice1 is equal to 2, then i want to add an extra line to the console output." So why add a second condition to your if statement?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

          int choice1;

          cout  <<  "Hi user, today i will do something really good for your computer!" << endl;
          cout  <<  "1. Ok" << endl;
          cout  <<  "2. What are you doing?" << endl;
          
          cin  >>  choice1;


          if( choice1 == 2 )
                    cout  <<  "Too Bad for you then!";

          cout  <<  "Ok then, let's start!";

          return 0;
}
Topic archived. No new replies allowed.