GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Oct-2009, 23:04
Freakzoyd Freakzoyd is offline
New Member
 
Join Date: Oct 2009
Posts: 14
Freakzoyd is on a distinguished road

Error: expected unqualified-id before "if"


Hey guys, I am new to c++ programming and i am making this program but i have some errors that i dont really understand what they mean. Please tell me what is wrong about it. The program is originally written in spanish but i traducted but the variables are still in spanish but i think they're easy to understand.

This are the errors i have:
52 - expected unqualified-id before "if"
52 -expected `,' or `;' before "if"
74 -expected unqualified-id before "if"
74 -expected `,' or `;' before "if"
108 -expected constructor, destructor, or type conversion before '(' token
108 -expected `,' or `;' before '(' token
109 -expected unqualified-id before "return"
109 -expected `,' or `;' before "return"
110 -expected declaration before '}' token

Here is the code (I marked the line numbers as coments):
CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int opcion1, opcion2, opcion3, opcion4, a, b, suma, resta, multi, cel, faren, altura, base, area, radio;
    cout<<"1.-Operations\n";
    cout<<"2.-Convertions\n";
    cout<<"3.-Areas\n";
    cout<<endl<<endl;
    cout<<"Eligie la opcion: ";
    cin>>opcion1;
    cout<<endl;
    if (opcion1==1){
                  cout<<"1.-Sum\n";}
                  cout<<"2.-Subtraction\n";
                  cout<<"3.-Multiplication\n";
                  cout<<endl<<endl;
                  cout<<"Elige la opcion: ";
                  cin>>opcion2;
                  cout<<endl<<endl;
                  if (opcion2==1){
                                  cout<<"Choose the first number: ";
                                  cin>>a;
                                  cout<<"Choose the second number: ";
                                  cin>>b;
                                  cout<<endl;
                                  suma=a+b;
                                  cout<<"The result is: "<<suma <<endl;
                                  }
                  if (opcion2==2){
                                  cout<<"Choose the first number: ";
                                  cin>>a;
                                  cout<<"Choose the second number: ";
                                  cin>>b;
                                  cout<<endl;
                                  resta=a-b;
                                  cout<<"The result is: "<<resta <<endl;
                                  }
                  if (opcion2==3){
                                  cout<<"Choose the first number: ";
                                  cin>>a;
                                  cout<<"Choose the second number: ";
                                  cin>>b;
                                  cout<<endl;
                                  multi=a*b;
                                  cout<<"The result is: "<<multi <<endl;
                                  }
    }
    if(opcion1==2){ //Line 52//
                    cout<<"1.-Convert Celsius to Fahrenheit\n";
                    cout<<"2.-Convert Fahrenheit to Celsius\n";
                    cout<<endl<<endl;
                    cout<<"Choose the option: ";
                    cin>>opcion3;
                    cout<<endl;
                    if (opcion3==1){
                                    cout<<"Temperature in Celsius: ";
                                    cin>>cel;
                                    cout<<endl;
                                    faren=((cel-32)/9)*5;
                                    cout<<"The temperature in Fahrenheit is: "<<faren <<endl;
                                    }
                   if (opcion3==2){
                                    cout<<"Temperature in Fahrenheit: ";
                                    cin>>faren;
                                    cout<<endl;
                                    cel=((faren*9)/5)+32;
                                    cout<<"The temperature in Celsius is: "<<cel <<endl;
                                    }
    }
    if(opcion1==3){ //Line 74//
                   cout<<"1.-Area of a Triangle\n";
                   cout<<"2.-Area of a Rectangle\n";
                   cout<<"3.-Area of a Circle\n";
                   cout<<endl<<endl;
                   cout<<"Choose an option: ";
                   cin>>opcion4;
                   cout<<endl;
                   if (opcion4==1){
                                  cout<<"Give a high: ";
                                  cin>>altura;
                                  cout<<"Give a base: ";
                                  cin>>base;
                                  cout<<endl;
                                  area=(base*altura)/2;
                                  cout<<"The area of the traigle is: "<<area <<endl;
                                  }
                   if (opcion4==2){
                                  cout<<"Give a high: ";
                                  cin>>altura;
                                  cout<<"Give a base: ";
                                  cin>>base;
                                  cout<<endl;
                                  area=base*altura;
                                  cout<<"The area of the rectangle is: "<<area <<endl;
                                  }
                   if (opcion4==3){
                                   cout<<"Give the Radio: ";
                                   cin>>radio;
                                   cout<<endl;
                                   area=(radio*radio)*3.1416;
                                   cout<<"The area of the Triangle is: "<<area <<endl;
                                   }
    }
 system("PAUSE"); //Line 108//
 return EXIT_SUCCESS; //Line 109//
 } //Line 110//

I would appreciate any help, I think this is a very simple mistake but because of my lack of experience i dont really understand it. Thx in advance
  #2  
Old 31-Oct-2009, 23:33
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,007
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Noob needs help


Did you mean to have this closing brace:
CPP / C++ / C Code:
     if (opcion1==1){
                  cout<<"1.-Sum\n";}    //<-- here
                  cout<<"2.-Subtraction\n";
                  cout<<"3.-Multiplication\n";
With that i get main() closing at line 51. I don't think that's what you had intended.
If that is removed things change quite a bit.

When first writing a new functions or programs indicate your blocks intentionally like this:
CPP / C++ / C Code:
  int blah = 1;
  if (blah)
  {
    cout << This gets done\n" ;
  }
The heck with saving a line,,, they're cheap! It makes it much easier to follow the flow.
You can always go back and collapse them after the new section is debugged.
Also , some editors (like vim) will hightlight both ends of a block when the cursor is over the { or }.
That's how I found the closing brace I mentioned above. Very handy.
Last edited by Howard_L : 01-Nov-2009 at 00:08.
  #3  
Old 01-Nov-2009, 00:46
Freakzoyd Freakzoyd is offline
New Member
 
Join Date: Oct 2009
Posts: 14
Freakzoyd is on a distinguished road

Re: Noob needs help


Hey tahnks this really helped me. It was a really fool mistake, so do you recomend me to download the vim editor? Do you know why after i run my program i got this message that only lets me see the result like for half a second?

  #4  
Old 01-Nov-2009, 10:37
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,007
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Error: expected unqualified-id before "if"


Quote:
It was a really fool mistake
No biggie, live and learn.
Quote:
do you recomend me to download the vim editor?
Well I like it , others don't. It operates in a uique fashion that takes some time to get used to. If you're running linux odds are you already have it installed. There are windows versions available too but I have not used them. Have you checked your editors' options to see if you can set it up to do enclosure highlighting?
Quote:
i run my program i got this message that only lets me see the result like for half a second?
What message? OS? Compiler?
  #5  
Old 01-Nov-2009, 13:13
Freakzoyd Freakzoyd is offline
New Member
 
Join Date: Oct 2009
Posts: 14
Freakzoyd is on a distinguished road

Re: Error: expected unqualified-id before "if"


I dont really know, the one of the screencshot of my last post
  #6  
Old 01-Nov-2009, 19:25
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,007
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Error: expected unqualified-id before "if"


You don't know? How can you not know?
Well from that I see that your're using the DevC++ IDE on XP.
What does "Presione una tecla para continuar ..." mean?
Would it by any chance be "Press a key to continue" ?
Is that all you see when you run the program you posted above after taking out the '}' I pointed out in the previous post?
Have you successfully run other C++ programs in your DevC setup?
Last edited by Howard_L : 01-Nov-2009 at 20:09.
  #7  
Old 01-Nov-2009, 20:58
Freakzoyd Freakzoyd is offline
New Member
 
Join Date: Oct 2009
Posts: 14
Freakzoyd is on a distinguished road

Re: Error: expected unqualified-id before "if"


Yeah, im alrready able to run a program. But when i run it and get the finall result, it only lets me see it for like half a second and then appears that gray message. Endeed budy, "Presione una tecla para continuar" means "Press a key to continue". Any reason why the gray message appear? Is it my Dev C++ version? Do you know where i can download a different one?
  #8  
Old 01-Nov-2009, 23:06
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,007
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Error: expected unqualified-id before "if"


Ok, I cranked up my old 98 and DevC 4 and I see now.
Yes the console closes because there is a \n left in stdin from the last cin >> xxx.
system("pause") . . sees the leftover \n and thinks you just pressed it and so moves on.

You could add a second system("pause") but instead I would recommend you use the C++ "getline() function as it is cross platform where system("pause") is windows only and that any system() call will fork another process which will cause more work for the cpu. (in fact, system("pause") causes my laptop to overheat if left paused for 5 minutes or so!)

So try this at the bottom of your prog:
CPP / C++ / C Code:
  //system("PAUSE"); //Line 108
  // system("PAUSE"); is a tool of the empire and cannot be trusted

  // instead use getline(); :
  cout << "Presione tecla de la Entrails (hey it's halloween) para continuar" << endl;
  string trash;
  getline(cin, trash); // this one will get the leftover \n from last cin
  getline(cin, trash); // this one will then "block" and serve as the pause

  return EXIT_SUCCESS; //Line 109//
} //Line 110//
I had fogotten how nice DevC++ is. I'm tempted to stay in windows a while.
Another option would be running your program in a separate MSDOS console instead of the pop up console DevC uses.
I think you will find it in Start > Accessories. Then you will have to navigate to the directory your prog.exe is in.
That way the console will be left open for you to look over your last output as long as you want and you wouldn't even need any of the pause stuff at all.
Type exit to close it when you're all done.
Last edited by Howard_L : 01-Nov-2009 at 23:49.
  #9  
Old 07-Nov-2009, 01:02
Freakzoyd Freakzoyd is offline
New Member
 
Join Date: Oct 2009
Posts: 14
Freakzoyd is on a distinguished road

Re: Error: expected unqualified-id before "if"


Hey sorry for asnwering late, i've been busy this last days. Thanks a lot for this information, it really worked an now it lets me see the result of my program as long as i want. Thx a lot a gain.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help a noob c_noob C Programming Language 1 09-Nov-2007 12:20
Total Noob needing help not a banana C++ Forum 4 04-Jun-2007 18:12
Noob with a question,any help would be greatly appreciated :) yrostran C Programming Language 7 11-May-2006 02:44
Noob C++ question Capnpooh C++ Forum 2 30-Mar-2006 19:25
Noob question on c arrays and functions brett C Programming Language 1 20-Apr-2005 03:59

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:47.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.