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. -
#include <iostream>
-
using namespace std;
-
-
int main()
-
{
-
float n;
-
-
cout << "Please enter a value: ";
-
cin >> n;
-
cout << endl;
-
-
if (n ==0)
-
cout<< "Your value is zero.";
-
cout << endl;
-
-
else if (n < 0)
-
n= n * (-1);
-
cout << "The absolute value for this number is "<<n<<".";
-
-
else (n > 0)
-
cout <<"Your value is positive.";
-
-
system("PAUSE");
-
return 0;
-
}
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...
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.
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.
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.
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: - if (n == 0) {
-
// Your statement here.
-
// Your second statement here.
-
// Your third statement here.
-
} else if (n > 0) {
-
// Etc.
-
}
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?"
It works now...I placed curly brackets differently(not the way you showed it in your post).
Thank you.
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: - #include<iostream>
-
using namespace std;
-
-
int main()
-
{
-
system("TITLE Calculator");
-
system("COLOR 2");
-
char cChar;
-
double dfirstnumber;
-
double dsecondnumber;
-
char cDoagain;
-
-
do
-
{system("TITLE Calculator");
-
system("CLS");
-
cout<<"Please enter the first number you would like to use:"<<endl;
-
cin>> dfirstnumber;
-
cout<<"Please enter the operation you would lik to complete"<<" (+,-,* oder /)"<<endl;
-
cin>> cChar;
-
cout<<"Please enter the second number you would like to use"<<endl;
-
-
switch (cChar)
-
{
-
case '+': ;
-
cout<<"The result is: "<<dfirstnumber <<"+" <<dsecondnumber <<"="<<(dfirstnumber + dsecondnumber)<<endl;
-
break;
-
case '-': ;
-
cout<<"The result is: "<<dfirstnumber <<"-" <<dsecondnumber <<"="<<(dfirstnumber - dsecondnumber)<<endl;
-
break;
-
case '*': ;
-
cout<<"The result is: "<<dfirstnumber <<"*" <<dsecondnumber <<"="<<(dfirstnumber * dsecondnumber)<<endl;
-
break;
-
case '/': ;
-
if(dsecondnumber == 0);
-
{
-
cout<<"That is an invalid operation"<<endl;
-
}
-
-
else
-
{
-
cout<<"The result is: "<<dfirstnumber <<"/" <<dsecondnumber <<"="<<(dfirstnumber / dsecondnumber))<<endl;
-
}
-
break;
-
default:
-
cout<<"That isan invalid operation"<< endl;
-
break;
-
}
-
cout<<"Would you like to start again? (Y or N)"<<endl;
-
cin>> cDoagain;
-
}while (cDoagain =='Y' || cDoagain == 'y');
-
return 0;
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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 ...
|
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...
|
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
|
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">";
| |
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...
|
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());
|
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.
|
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
|
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
|
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,...
| |
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...
|
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |