473,651 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error: unexpected primary-expression before "else"

5 New Member
I'm supposed to write a program that is going to calculate absolute value of given number(and print appropriate message) but I'm having tough time with the statements(expe cted primary-expression before "else").I tried to fix them but I simply cannot find the right answer.Any help or hints would be greatly appreciated.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. float n;
  7.  
  8. cout << "Please enter a value: ";
  9. cin >> n;
  10. cout << endl;
  11.  
  12. if (n ==0)
  13.    cout<< "Your value is zero.";
  14.    cout << endl;
  15.  
  16. else if   (n < 0)
  17.         n= n * (-1);
  18.         cout << "The absolute value for this number is "<<n<<".";
  19.  
  20. else   (n > 0)
  21.      cout <<"Your value is positive.";
  22.  
  23. system("PAUSE");
  24. return 0;
  25. }
Oct 7 '07 #1
8 6691
Ganon11
3,652 Recognized Expert Specialist
Are you sure you've posted your code correctly? I don't see a problem with this...
Oct 7 '07 #2
MLZ242
5 New Member
My problem is that when I try to run the program error comes up expected primary-expression before "else" .

I tried to changed the else statements to if but when I test the program I get 2 responses instead of one.
Oct 7 '07 #3
oler1s
671 Recognized Expert Contributor
You forgot the braces around the if and else statements. Code indentation and other formatting standards help in spotting these errors.
Oct 7 '07 #4
MLZ242
5 New Member
You forgot the braces around the if and else statements. Code indentation and other formatting standards help in spotting these errors.
I tried that before it didn't work.Anyway that you for the response.
Oct 7 '07 #5
Ganon11
3,652 Recognized Expert Specialist
No, now that you mention it, that's the problem. You have more than 1 statement under your first if... statement, so only the first statement is treated as being under the if clause. When you have more than one statement in the if...clause, you need to wrap them in curly braces:

Expand|Select|Wrap|Line Numbers
  1. if (n == 0) {
  2.    // Your statement here.
  3.    // Your second statement here.
  4.    // Your third statement here.
  5. } else if (n > 0) {
  6.    // Etc.
  7. }
The error you are getting is due to the fact that, once your program gets to the else statement, there's no if statement to connect to it - so it says, "Hey, I expected something before this else, and I didn't see anything - what's up with that?"
Oct 7 '07 #6
MLZ242
5 New Member
It works now...I placed curly brackets differently(not the way you showed it in your post).

Thank you.
Oct 7 '07 #7
yabbadabbaduh
1 New Member
I get the same error but I don't know what I could do to fix it. I was trying to make a simple calculator and have to use the if/else thing but i can't figure out how to handle that error. This is my code:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     system("TITLE Calculator");
  7.     system("COLOR 2");
  8.     char cChar;
  9.     double dfirstnumber;
  10.     double dsecondnumber;
  11.     char cDoagain;
  12.  
  13.     do
  14.     {system("TITLE Calculator");
  15.         system("CLS");
  16.         cout<<"Please enter the first number you would like to use:"<<endl;
  17.         cin>> dfirstnumber;
  18.         cout<<"Please enter the operation you would lik to complete"<<" (+,-,* oder /)"<<endl;
  19.         cin>> cChar;
  20.         cout<<"Please enter the second number you would like to use"<<endl;
  21.  
  22.         switch (cChar)
  23.         {
  24.         case '+': ;
  25.             cout<<"The result is: "<<dfirstnumber <<"+" <<dsecondnumber <<"="<<(dfirstnumber + dsecondnumber)<<endl;
  26.         break;
  27.         case '-': ;
  28.             cout<<"The result is: "<<dfirstnumber <<"-" <<dsecondnumber <<"="<<(dfirstnumber - dsecondnumber)<<endl;
  29.         break;
  30.         case '*': ;
  31.             cout<<"The result is: "<<dfirstnumber <<"*" <<dsecondnumber <<"="<<(dfirstnumber * dsecondnumber)<<endl;
  32.         break;
  33.         case '/': ;
  34.         if(dsecondnumber == 0);
  35.         {
  36.         cout<<"That is an invalid operation"<<endl;
  37.         }
  38.  
  39.         else
  40.         {
  41.         cout<<"The result is: "<<dfirstnumber <<"/" <<dsecondnumber <<"="<<(dfirstnumber / dsecondnumber))<<endl;
  42.         }
  43.         break;
  44.         default:
  45.             cout<<"That isan invalid operation"<< endl;
  46.         break;
  47.         }
  48.     cout<<"Would you like to start again? (Y or N)"<<endl;
  49.     cin>> cDoagain;
  50.     }while (cDoagain =='Y' || cDoagain == 'y');
  51.     return 0;
  52. }
Aug 20 '08 #8
Ganon11
3,652 Recognized Expert Specialist
After your default statement, you have a break; - it is unnecessary, as the default is already at the end of the switch block. The break statements in the other cases are there to skip the remainder of the block and go to the end - at the default, there is nothing to skip, so the break is useless.

Also, every case, you type:

case '+': ;

The semicolon is also unnecessary. It might not cause problems, but it is just an empty statement, so it definitely won't cause any problems when removed.
Aug 20 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
19023
by: Ehartwig | last post by:
I recently created a script for user verification, solved my emailing issues, and then re-created the script in order to work well with the new PHP 5 that I installed on my server. After submitting user information into my creation script, I get the following error from the page that is suppose to insert the user data into the database, create a code, then send an email out for verification. Parse error: parse error, unexpected $end in...
8
46180
by: Wescotte | last post by:
The error message Parse error: syntax error, unexpected $end in FILE on line X is one I run into frequently and I know the cause is I missed an ending quote. Is there an easy way to determine where the inital " started? I find myself adding /* */ blocks or cutting/pasting sections of code out in order to find where the error occured. Wouldn't it it be nice if the warning message included the line in teh source where the initial quote ...
5
13156
by: Anna MZ | last post by:
I am new to php and have written the following mysql code to enter the details of a new user in the admin subdomain of my website: $sql = "INSERT INTO 'users' ('userid', 'username', 'upassword') VALUES ('$_POST', '$_POST', '$_POST') mysql_query($sql)"; When I view the code in Internet Explorer I get the following error message: Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or...
1
6842
by: soidariti | last post by:
database error - parse error, unexpected $, expecting kEND AddUserAuthorisationToUsers.rb migration file 1. class AddUserAuthorisationToUsers < ActiveRecord::Migration 2. def self.up 3. drop_table :users 4. 5. create_table :users, :options => 'ENGINE=InnoDB DEFAULT CHARSET=utf8' do |t| 6. t.column :first_name, :string
4
22146
kestrel
by: kestrel | last post by:
I have some html code that is supposed to be displayed by php echo. But for some reason i keep getting a syntax error, and i cant figure out what is going on. Heres what i have <?php if(isset($_GET)) { echo "<div id="visible">"; echo "<span onclick="swapform()">Log In Form</span>"; echo "</div>"; echo "<div id="theform" style="visibility: hidden">";
3
1333
by: nazgul42 | last post by:
I am writing a very simple login script for a website that I am also writing, but when I try to run it, the only error I get is: Parse error: parse error, unexpected $ in /home/www/zammarket.freehostia.com/signup.php on line 22 Line 22 is the last line in the file. My code is here: <?php echo "Starting..."; @mysql_connect(My_Sql_Server, My_DB, My_Password) or die("Cannot Connect To DB!"); @mysql_select_db(My_DB) or die("Cannot...
1
1395
by: basswhizz | last post by:
Hi guys im having trouble with somethings else now can you help out thanks!! Im getting this error message Parse error: syntax error, unexpected $end Here's my code cheers!!] <?php // Connects to your Database mysql_connect("aldridge3", "root", "") or die(mysql_error()); mysql_select_db("dwalk96") or die(mysql_error());
3
5186
by: brkseven | last post by:
Looking for help with this Contact Form. The error is on line 1, but that' doesn't mean a lot, I think. In fact, a php syntax check passed it, but I was hoping for an easy syntax error, it looks more complicated now. Anyway, here's the error: "Parse error: syntax error, unexpected T_VARIABLE in /home/xxx/public_html/yyy/zzz/contactengine.php on line 1" It's a contact form from css-tricks, when I submit that's what I get.
5
4216
by: vultren | last post by:
Parse error: syntax error, unexpected $end in I keep getting that, I have no clue where to fix it. Any help would be VERY APPRECIATED! <?php if(isset($_POST)) { // EDIT THE 2 LINES BELOW AS REQUIRED
5
1928
by: Adam Pelling | last post by:
I'm getting this error Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/neblncbt/public_html/forum/includes/acp/acp_board.php on line 69 Here is the acp_board.php file <?php /** * * @package acp
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8697
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7297
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.