error expected unqualifed-id before '{' token [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int num1, num2, num3, num4, num5, result, result1, result2, result3, value, cont;
//number user enters/variable
int main()
{
cout << "please enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
;cin >> value
;cout << "please enter the first number number:";
cin >> num1
;cout << "please enter the second number: ";
cin >> num2
;if(value == '+' )
;result = num1 + num2;
cout << num1 << " plus " << num2 << " is equal to: " << result << ".\n";
if(value = '-' )
;result = num1 - num2;
cout << num1 << " minus " << num2 << " is equal to: " << result << ".\n";
if(value = '*' )
;result = num1 * num2;
cout << num1 << " times " << num2 << " is equal to: " << result << ".\n";
if(value = '/' )
;result = num1 / num2;
cout << num1 << " divided by " << num2 << " is equal to: " << result << ".\n";
}
{
cout << "press 1 to enter more numbers, or press 0 to not"
cin >> cont
if(cont = 1)
cout << "please enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
cin >> value1
cout << "please enter the next number:";
cin num3
else(cont = 0)
else(value1 = +)
;result1 = result + num3;
cout << result << " plus " << num3 << " is equal to: " << result1 << ".\n";
else(value1 = -)
;result1 = result - num3
cout << result << " minus " << num3 << " is equal to: " << result1 << ".\n";
else(value1 = *)
;result1 = result * num3
cout << result << " times " << num3 << " is equal to: " << result1 << ".\n";
else(value1 = /)
;result1 = result / num3
cout << result << " divided by " << num3 << " is equal to: " << result1 << ".\n";
}
{
cout << "press 1 to enter more numbers, or press 0 to not"
cin >> cont
if(cont = 1)
cout << "please enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
cin >> value1
;cout << "please enter the next number: ";
cin >> num4
else(cont = 0)
else(value2 = +)
;result2 = result1 + num4;
cout << result1 << " plus " << num4 << " is equal to: " << result2 << ".\n";
else(value2 = -)
;result2 = result1 + num4;
cout << result << " minus " << num3 << " is equal to: " << result1 << ".\n";
else(value2 = *)
;result2 = result1 * num4
cout << result << " times " << num3 << " is equal to: " << result1 << ".\n";
else(value2 = /)
;result2 = result1 / num4
cout << result << " divided by " << num3 << " is equal to: " << result1 << ".\n";
{
cout << "press 1 to enter more numbers, or press 0 to not"
cin >> cont
if(cont = 1)
cout << "enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
cin >> value2
;cout << "please enter the next number: ";
cin >> num5
if(value3 = +)
;result3 = result2 + num5;
cout << result2 << " plus " << num5 << " is equal to: " << result3 << ".\n";
else(value3 = -)
;result3 = result2 - num5
cout << result << " minus " << num3 << " is equal to: " << result1 << ".\n";
else(value3 = *)
;result3 = result2 * num5
cout << result << " times " << num3 << " is equal to: " << result1 << ".\n";
else(value3 = /)
;result3 = result2 / num5
return 0;
the error happens at line 34, where it says {
so please help me!
the code is for a basic calculator
feel free to use it if you can correct line 34!
I have no idea what is causeing it
I am a noob to the c++
codeing so please help!
I have done my own research and I cannot find it.

You can't say stuff like
else(value1 = +)
in C++. You must mean something else, but it is hard to guess what because there are so many errors in your code. In general, you cannot just type random characters and expect a functioning program.

The error stems from the additional { ... } blocks following the main function, since the compiler does not know what to do with code outside of declarations . But that is not the only problem with your code:
Putting a semicolon directly after an if statement means "if the condition is true do nothing anyway", and the next statement is executed either way.
if(value = +) should be if(value == '+') etc - you mixed the association = with comparison ==, plus you try using an operator + instead of a character '+'* what is else(something) supposed to do? Code blocks are put in {}s, not ()s
May I suggest you start programming in an easier language like Python? Its meaningful indentation and the lack of semicolons makes life a lot easier...

Get rid of all of the
}
{
and you'll be able to proceed to fixing your next error.
When you write braces like the following: }
you're closing your block of code, which in this case is your main function.
and when you write the following: {
The compiler thinks that you're trying to start a new function, but there's not function signature and you get an error.
It looks to me like you intend all of this code to be inside of your main function, so you want something like the following:
int main()
{
//insert all of your code here
return 0;
}

Related

Why does my cacluator repeat the same output when I use a decimal value?

I have written this code. It is working good, I am curious though. When I add a decimal number in one of the functions why does the rest of my functions repeat the same code and finish? I know it works when I assign the number input as double. I am curious though why it funtions like this.
#include <iostream>
#include <cmath>
using namespace std;
int num1, num2;
int request(){
cout << "Type in a number: " << flush;
cin >> num1;
cout << "Type in a number: " << flush;
cin >> num2;
}
int getMin(){
cout << "Get the minimum of 2 numbers" << endl;
request();
if(num1 < num2)
cout << "The minimumm of " << num1 << " and "
<< num2 << " is " << num1 << endl;
else
cout << "The minimumm of " << num1 << " and "
<< num2 << " is " << num2 << endl;
}
int getMax(){
cout << "Get the maximum of 2 numbers" << endl;
request();
if(num1 > num2)
cout << "The maximum of " << num1 << " and "
<< num2 << " is " << num1 << endl;
else
cout << "The maximum of " << num1 << " and "
<< num2 << " is " << num2 << endl;
}
int power(){
cout << "Get the exponent of 2 numbers" << endl;
request();
cout << num1 << " to the power of " << num2 << " is "
<< pow(num1,num2) << endl;
}
int main(){
getMin();
cout << endl;
getMax();
cout << endl;
power();
cout << endl;
}
output
Get the minimum of 2 numbers
Type in a number: 5.5
Type in a number: The minimumm of 5 and 0 is 0
Get the maximum of 2 numbers
Type in a number: Type in a number: The maximum of 5 and 0 is 5
Get the exponent of 2 numbers
Type in a number: Type in a number: 5 to the power of 0 is 1
why does the rest of my functions repeat the same code and finish?
It's because when the extraction of an int fails, the character making the extraction failing is left in the stream (and will be encountered every time you try a new extraction) and the stream is set in a failed state making any further attempts to extract data from the stream failing - so whatever values you've previously set for num1 and num2 will be reused over and over. If no value was assigned, you read uninitialized memory and your program has undefined behavior.
To recover from a failed extraction you can clear() the stream's state flags and ignore() the characters in the stream until end of line.
Also, your request() function and the rest of the functions have undefined behavior anyway. They are declared to return an int but does not return anything, so make them void instead.
You could however change request() to return a bool. Return true if the requrest succeeded and false otherwise:
#include <limits> // std::numeric_limits
bool request() {
cout << "Type in a number: ";
if(cin >> num1) {
cout << "Type in a number: ";
if(cin >> num2) return true; // both numbers extracted successfully
}
// something failed
if(not cin.eof()) { // skip clearing the state if eof() is the reason for the failure
cin.clear(); // clear the fail state
// ignore rest of line to remove the erroneous input from the stream
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return false;
}
You can now use it and make sure that the user actually successfully entered the two values. Example:
void getMin() { // made it void since it doesn't return anything
cout << "Get the minimum of 2 numbers" << endl;
if(request()) { // check that extraction succeeded
if(num1 < num2)
cout << "The minimumm of " << num1 << " and "
<< num2 << " is " << num1 << endl;
else
cout << "The minimumm of " << num1 << " and "
<< num2 << " is " << num2 << endl;
}
}

Ignore a char if present and continue reading int value of various sizes

I'm writing a math quizzing program that does addition, subtraction, multiplication, and division. In my subtraction function, I can't figure out how to ignore a user inputted "-" sign on the cin >> unserInput portion, as it's not always required (like if it's positive).
I have an if statement that if the machine calculated resultant is negative, to have the ability to cin the "-" char, but the program crashes if the user omits it. How can I validate if the "-" char is present, ignore it if it is and continue to pull in int values, and if it's not present, just pull in the values?
void subFractions(int num1 , int num2 , int denom1 ,int denom2 ,int answerNum
, int answerDenom , int resultNum , int resultDenom , int number , int
numCorrect = 0)
{
// use random number to work
srand(time(NULL));
// begin counter
number = 1;
// count number of math problems to be pushed to user
while ( number < 11)
{
//Assigning the numerators and denominators to random numbers
//between 1 to 10
num1 = rand() % 10 + 1; // numerator 1
num2 = rand() % 10 + 1; // numerator 2
denom1 = rand() % 10 + 1; // denom 1
denom2 = rand() % 10 + 1; // denom 2
char sign; // operator sign (/)
char neg; // pull negative sign in if required
// push header to user
cout << "Question " << number << ": What is the result of the
following?" << endl;
cout << right << setw(3) << num1 << right << setw(8) << num2 << endl;
cout << right << setw(3) << " ---" << right << setw(3) << "-" <<
right << setw(5) << " ---" << endl;
cout << right << setw(3) << denom1 << right << setw(8) << denom2 <<
endl;
//Calculate resultant
resultNum = ( ( num1 * denom2 ) - (denom1 * num2 ) );
resultDenom = ( denom1 * denom2 );
double resultant = resultNum / resultDenom;
//getting the answer from the user
cout << "Enter your answer in the form numerator / denominator: ";
// check resultant for negative number
if (resultant < 0)
{
cout << "neg answer" << endl;
cin >> neg >> answerNum >> sign >> answerDenom;
answerNum = (-1) * answerNum;
cout << endl;
} // end negative number check
else
{
cout << "pos answer" << endl;
cin >> answerNum >> sign >> answerDenom;
cout << endl;
} // end positive number check
double userResultant = answerNum / answerDenom;
//if the answer is right
if (resultant == userResultant)
{
cout << "Correct!" << endl;
numCorrect++;
} // end correct statement
else
{
cout << "That is not correct. " << endl;
//Reshowing the fraction with answer of the numerator
cout << right << setw(3) << num1 << right << setw(8) << num2 <<
right << setw(8) << resultNum << endl;
//Showing the fraction and the symbols
cout << right << setw(3) << " ---" << right << setw(3) << "-" <<
right << setw(5) << " ---" <<
right << setw(3) << "=" << right << setw(5) << "---" << endl;
//Showing the denominator with answer of the denominator
cout << right << setw(3) << denom1 << right << setw(8) << denom2
<< setw(8) << resultDenom << endl;
cout << "" << endl;
} // end incorrect statement
If the user does not enter the "-", it should kick out the incorrect statement. As it is, it's just crashing. Presumably because it's loading values into the incorrect variable and leaving the last one empty.

C++ switch statement for arithmetic operations

When I run my program, it doesn't allow me to pick an operation. It just goes straight to "Invalid option" and asks again. I want to be able to choose '+', '-', '*', '/', '^', and '!' as my options. What did I do wrong?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
char operation;
int num1, num2, result, remain;
//Display the menu to the user and get their first choice
cout << "What operation would you like to perform:" << endl
<< " + addition\n - subtraction\n * multiplication\n / division\n ^ number to power\n ! factorial"
<< "\n q quit" << endl << endl << "Operation? ";
cin >> operation;
//Switch - the user does not want to quit
switch (operation)
{
case 1: result=num1+num2;
cout << "Enter the first number to add: " << endl;
cin >> num1;
cout << "Enter the second number to add: " << endl;
cin >> num2;
cout << endl << num1 << " + " << num2 << " = " << result;
break;
case 2: result=num1-num2;
cout << "Enter the first number to subtract: " << endl;
cin >> num1;
cout << "Enter the second number to subtract: " << endl;
cin >> num2;
cout << endl << num1 << " - " << num2 << " = " << result;
break;
case 3: result=num1*num2;
cout << "Enter the first number to multiply: " << endl;
cin >> num1;
cout << "Enter the second number to multiply: " << endl;
cin >> num2;
cout << endl << num1 << " * " << num2 << " = " << result;
break;
case 4: result=num1/num2;
cout << "Enter the dividend: " << endl;
cin >> num1;
cout << "Enter the divisor: " << endl;
cin >> num2;
cout << endl << num1 << " / " << num2 << " = " << result;
cout << endl << num1 << " % " << num2 << " = " << result;
break;
case 5: result=num1^num2;
cout << "Enter the base number " << endl;
cin >> num1;
cout << "Enter the power: " << endl;
cin >> num2;
cout << endl << num1 << " ^ " << num2 << " = " << result;
break;
case 6: result=num1!=num2;
cout << "Enter a number: " << endl;
cin >> num1;
cout << endl << num1 << " ! " << " = " << result;
break;
default:
cout << "That is an invalid operation!" << endl;
break;
} // switch statement closed
cout << endl << "What operation would you like to perform:" << endl << " + addition\n - subtraction\n * multiplication\n / division\n ^ number to power\n ! factorial" << "\n q quit" << endl << endl << "Operation? ";
cin >> operation;
return 0;
} //main statement closed
You are using a char against a switch of ints that do not match character codes that you wish to select. If you would like to decide among characters, use character literals instead of integer numbers:
case '+':
// Read inputs first
cout << "Enter the first number to add: " << endl;
cin >> num1;
cout << "Enter the second number to add: " << endl;
cin >> num2;
// Compute the result next
result=num1+num2;
cout << endl << num1 << " + " << num2 << " = " << result;
break;
Note that the assignment
result=num1+num2;
should come after the code that reads the inputs, i.e. num1 and num2.
at the end when I ask if it wants to do another operation, it just ends even if I pick an operation.
This is because an end-of-line character that you entered after num2 is sitting in the buffer. You need to ignore it. Add this line:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
before
cin >> operation;

Is there a way to hide variables in cout (C++)?

I'm having an issue figuring out how to hide a specific variable (if that is even possible) in a cout function. Basically I need to do a number guess game, which would be easy enough except our teacher wants us to do it like a random math equation. Which... would honestly still be fairly easy except the way we have to do it is the program has to randomly create the problem and then randomly pick one of the 3 numbers to display and the user has to guess the other two missing numbers. For example if the program picked 20 + 32 = 52 it could potentially display __ + 32 = __.
I've gotten that far however I can't figure out how to make it so it displays like that but still allows me to put the line something like this
cout << num1 //Hidden << " + " << num2 << " = " << num3 //Hidden
However like I said I don't even know if that is possible if not then I will probably have to rewrite the whole program. This is what I have so far:
int main()
{
int num1, num2, num3, random1, guess1, guess2;
string play = "";
cout << "Would you like to run the number guessing program? (enter yes or no): ";
getline(cin, play);
for (int i = 0; i < play.length(); i++)
{
play[i] = tolower(play[i]);
}
//Random seed
srand(time(0));
while (play == "yes")
{
//Generate random numbers and num3
num1 = 1 + rand() % 50 + 1;
num2 = 1 + rand() % 50 + 1;
num3 = num1 + num2;
int pickRandom[3] = { num1, num2, num3 };
//Display random elements
random1 = pickRandom[rand() % 3];
if (random1 == num1){
cout << "\nYour randomly generated number problem: " << num1 << " + " << "__" << " = " << "__" << endl;
}
if (random1 == num2){
cout << "\nYour randomly generated number problem: " << "__" << " + " << num2 << " = " << "__" << endl;
}
if (random1 == num3){
cout << "\nYour randomly generated number problem: " << "__" << " + " << "__" << " = " << num3 << endl;
}
//Get Guesses
cout << "\nBased off of this information please make an educated guess as to what the two missing numbers are.";
cout << "\n\nGuess for number 1 (between 1 and 100): ";
cin >> guess1;
while ((guess1 > 100) || (guess1 < 0))
{
cout << "\nSorry you need to enter an integer between 1 and 100" << endl;
cout << "\nGuess for number 1 (between 1 and 100): ";
cin >> guess1;
}
cout << "\n\nGuess for number 2 (between 1 and 100): ";
cin >> guess2;
while ((guess2 > 100) || (guess2 < 0))
{
cout << "\nSorry you need to enter an integer between 1 and 100" << endl;
cout << "\nGuess for number 2 (between 1 and 100: ";
cin >> guess2;
}
if (guess1 == )
}
return 0;
}
I don't think you can hide variables in cout. But you can use a variable instead of hardcoding "__".
For instance, you can simply write this:
if(guessed_var1_correctly)
var1 = num1
else
var1 = "__"
if(guessed_var2_correctly)
var2 = num2
else
var2 = "__"
if(guessed_var3_correctly)
var3 = num3
else
var3 = "__"
cout << "\nYour randomly generated number problem: " << var1 << " + " << var2 << " = " << var3" << endl;
where var1, var2, var3 are output variables. If the player guesses it correctly, it'll display the actual value num1, num2, or num3. If not, it'll simply display "__".

How to ask for user input in nested loops?

I was given the task of displaying Fibonacci numbers, but while asking the user how many number he/she would like to compute at a given time.
There was an example in the book they told me to refer. I figured a few lines of change in the code would produce the answer to my problem, but I'm having trouble understanding where I went wrong with this code.
int main()
{
int NumsToCal = 5;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;
int Num1 = 0, Num2 = 1;
char WantMore = '\0';
cout << Num1 << " " << Num2 << " " ;
do
{
for( int Index = 0; Index < NumsToCal; ++Index)
{
cout << Num1 + Num2 << " ";
int Num2Temp = Num2;
Num2 = Num1 + Num2;
Num1 = Num2Temp;
}
cout << "Do you want more numbers (y/n)? " << endl;
cin >> WantMore;
} while (WantMore == 'y');
cout << "Goodbye!" << endl;
return 0;
}
Xsami is absolutely right. You only need to include one more line like:
cin>>NumstoCal;
Though it won't be bad to change the way you output stuff for a bit more clarity.
Here is my code:
https://ideone.com/BXREP9
The only thing that you have to do is read NumsToCal again, and you have to do something like this after cin >> WantMore;
if ( WantMore == 'y' )
{
Num1 = 0;
Num2 = 1;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << Num1 << " " << Num2 << " " ;
}
This is my code: http://ideone.com/a8um5Z