Expected primary expression before ';' token - new to c++ [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So, I'm tasked with creating a simple calculator, that firstly will take 1 number, than the action you want to perform on it, and than the second number, problem is, I can't get it to work because (at least I think its because of that) the compiler can't take +, -, *, / as a char. what can I do to solve it?
allot of thanks in advance, I've tried to search a solution for a while now and couldn't...
#include <iostream>
using namespace std;
int main()
{
double first;
double second;
char x;
char add = "+" ;
char take = "-" ;
char add2 = "*" ;
char take2 = "/";
cout << "Please enter the first number\n";
cin >> first;
cout << "Please enter the math action\n";
cin >> x;
cout << "Please enter the second number\n";
cin >> second;
; if (x == add)
{
cout << first << x << second << "=" << first+second;
}
if (x == take)
{
cout << first << x << second << "=" << first-second;
}
if (x == add2)
{
cout << first << x << second << "=" << first*second;
}
if (x == take2)
{
cout << first << x << second << "=" << first/second;
}
else
{
cout << "Couldn't reconize the character, please try again";
}
}

There are two things you should correct here.
Use char c = 'x'; instead of char c = "x"; to set a character: double quotes give you a string, and single quotes give you a character.
There's a stray semicolon at the beginning of one of the lines half way through.

You need to remove this ; first from
; if (x == add)
and use single qoute ' while assigning char instead of "

Related

Identifier response is undefined and expected a ; [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
In my c++ course, I have been asked to generate a random number between 1 and 12 and add it to a sum. The program will ask if the user wants to add another number with the input y or n, and if the input is y it will add another number. When the sum reaches 50, the program will automatically end.
However, on lines 14 and 27, I get the error:
Expected a ;
And on line 16 I get the error:
identifier response is undefined
#include <iostream>
#include <iomanip>
using namespace std;
void rng(){
int rng = ((rand() % 12) + 1);
cout << rng;
}
int main()
{
int sum = 0
char response;
cout << "Would you like to add a number? y or n";
cin << response;
while (response = 'y') {
cout << "Would you like to add a number? y or n";
cin << response;
cout << "Okay then, the current sum is" << +" " << +sum << +rng;
if (response = 'n')
cout << "Okay thanks, have a good one";
if (sum > 50)
cout << "Alright the sum has reached 50 that seems like enough"
break
};
}
There are a number of mistakes in your code - some related to syntax errors, and some related to logic issues.
Regarding the syntax errors:
int sum = 0
needs to be
int sum = 0;
A semicolon ends a statement.
cin << response;
needs to be
cin >> response;
operator<< is used for output, whereas operator>> is used for input.
cout << "Okay then, the current sum is" << +" " << +sum << +rng;
needs to be
cout << "Okay then, the current sum is " << sum;
Using + on sum is redundant, but using + on rng is just plain wrong, since rng is a function, not a variable.
cout << "Alright the sum has reached 50 that seems like enough"
needs to be
cout << "Alright the sum has reached 50 that seems like enough";
Again, a semicolon ends a statement.
break
needs to be
break;
Again, a semicolon ends a statement.
Regarding the logic issues:
there is no need for #include <iomanip>, as you are not utilizing any stream manipulators.
you are missing #include <cstdlib> for rand().
rng() is a function, but the statement cout << ... << +sum << +rng; is trying to print it out as if it were a variable instead. The project requirement is to add the random number to the running sum, so rng() should return the number it generates, and then main() can add that number to sum before printing it out.
you are not calling srand() to initialize the random number generator before calling rand() the 1st time.
while (response = 'y') needs to be while (response == 'y'). operator= is for assignment, operator== is for comparison. That said, you should use a do..while loop instead, as there is no point in prompting the user twice on the 1st loop iteration.
if (response = 'n') needs to be if (response == 'n'). Again, assignment vs comparison. Also, you are also not break'ing the loop if the user enters 'n'.
if (sum > 50) is missing {} braces, so the subsequent break is not part of the if, and thus it will be invoked unconditionally after the 1st loop iteration is done, so the user will never be able to enter more than 1 number.
your cout statements should have \n or std::endl at the end of them, to print out a line break.
With that said, try something more like this instead:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int rng(){
return (rand() % 12) + 1;
}
int main()
{
srand(time(NULL));
int sum = 0;
char response;
do {
cout << "Would you like to add a number? y or n: ";
cin >> response;
if (response == 'n') {
cout << "Okay thanks, have a good one\n";
break;
}
if (response == 'y') {
sum += rng();
cout << "Okay then, the current sum is " << sum << "\n";
if (sum > 50) {
cout << "Alright the sum has reached 50 that seems like enough\n";
break;
}
}
}
while (true);
}
This line:
int sum = 0
Needs a semi-colon at the end.
Same with these lines:
cout << "Alright the sum has reached 50 that seems like enough"
break
You need to use == instead of = in the condition of the while statement:
while (response == 'y') {
Also, this line:
cout << "Okay then, the current sum is" << +" " << +sum << +rng;
should be
cout << "Okay then, the current sum is" << " " << sum << rng;

Why does this code return a long number at the end? (c++) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
#include <iostream>
using namespace std;
int funky(int stuff)
{
int number;
number = 10;
cout << "Please enter a number \n";
cin >> number;
stuff = number + 777;
cout << "The result is " << stuff << endl;
return 0;
}
int main()
{
int num1, num2;
funky(num1);
funky(num2);
cout << num1 << " and " << num2;
return 0;
}
I am trying to learn to use functions in C++, but i cannot figure out why this code returns a very long negative number at the end . can someone tell me why num2 returns a very long number? it doesnt make sense to me at all
Apparently, your intention was to assign stuff inside the function and use as num1 and num2 in main(). To do it, you need to change your parameter type from int to int&, i.e. pass by reference. Your function declaration should be int funky(int& stuff).
The way you defined the function, variables num1 and num2remain uninitialized, that's why cout << num1 << " and " << num2; is printing garbage.
There are a few things that are wrong with your code . For one, you do not need a value-returning function. You could instead use void.
You also are getting Undefined Behavior by not initializing your variables int num1 and int num2.
Now, an alternative to your problem would be using void. (If you only need two numbers.) Other than that I suggest using #Eugene's answer.
Replace:
int funky(int stuff) to void funky(int stuff).
For Example:
#include <iostream>
using namespace std;
void funky()
{
int num1 = 0;
int num2 = 0;
cout << "Please enter a number \n";
cin >> num1;
int Res1 = num1 + 777;
cout << "The result is " << Res1 << endl;
cout << "Please enter one more number \n";
cin >> num2;
int Res2 = num2 + 777;
cout << "The result is " << Res2 << endl;
cout << num1 << " and " << num2;
}
int main()
{
funky();
return 0;
}

Me not understanding do while loops [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
So i tried to make my first console aplication but it came to a bit of a bummer since i dont understand how a do while loop works
#include <iostream>
int balance = 100;
int pay = 30;
int awnser;
// Variables for the awnsers
int withdrawal;
int a = 1;
int main() {
do {
std::cout << "\n Whats the action you wanna do? \n 1 = Check balance \n 2 = Withdraw money \n 3 = Deposit money \n 4 = Check transaction history \n 5 = Exit \n";
std::cout << " ";
std::cin >> awnser;
if (awnser == 1) {
std::cout << balance << " Euros\n \n";
}
if (awnser == 2) {
std::cout << "How much do you wanna with draw?\n";
std::cin >> withdrawal;
if (withdrawal > balance)
std::cout << "You dont have that much money.\n \n";
else {
std::cout << "Your current balance is: " << balance - withdrawal;
}
}
if (awnser == 3) {
std::cout << "We know you dont have enymore daam money you beggar so dont even try that\n \n";
}
if (awnser == 4) {
}
if (awnser == 5) {
std::cout << "Enter 0 to exit or 1 to go back\n";
std::cin >> a;
}
else if (a == 1) {
std::cout << "\n";
return 1;
}
} while (a == 1);
}
I thought it would get back to the top since no other "if" requierements were met and just give me the "Whats the action you wanna take again" but it just exits out so what am i doing wrong?
If the number input isn't one that you check (1 to 5) then you hit:
else if (a == 1) {
std::cout << "\n";
return 1;
}
which will enter the if (because a is 1), print a new line and return from main ending your run.

C++ beginner, switch isn't outputting first character of string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My task is to get a string with no spaces from the user and make the computer count the number of characters, letters, numbers, and special characters (i.e. !##$%^&*) However the program seems to be skipping the first character no matter what category this character falls under. note that it does count it in the number of characters just not in its category
example:
cin >> aZ12!#
output: 6 characters, 1 letter, 2 numbers, 2 special characters.
it always skips the first character.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char str[100]; // available character string max is 99 characters
int i;
int lett;
int num;
int spec;
cout << "Please enter a continuous string of characters with no spaces" << endl ;
cout << "(example: ASO#23iow$)" << endl << endl ; //shows an example and then adds a blank line
cout << "Enter your string: " ;
cin >> str ;
cout << endl ;
while(str[i] != 0)
{
switch(str[i])
{
case '0' ... '9':
i++ && num++;
break ;
case 'a' ... 'z':
i++ && lett++;
break ;
case 'A' ... 'Z':
i++ && lett++;
break ;
default :
i++ && spec++;
}
}
cout << "your string has " << i << " characters" << endl ;
//prints the number of numbers in the string
cout << "Your string has " << num << " numbers in it." << endl ;
cout << "Your string has " << lett << " letters in it." << endl ;
cout << "Your string has " << spec << " special characters." << endl ;
return 0 ;
In your code, int i is not initialized. Using it is Undefined Behaviour.
int i = 0;
The same goes for the rest of your variables.
Also this doesnt do what you think it does:
i++ && lett++;
This is not do both operations, its a Boolean operator. It employs something called short circuiting, which means if the first part of the && evalutes to false (ie 0), then the expression must be 0 so there is no point in evaluating the rest of it (ie the lett++ part). So for your first loop (i == 0) your lett++ will be short circuited.
Change these to:
i++;
lett++;
If you fix this up it will work:
Live example

Can you add variables inside "cout" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm new to C++, on the chapter 2 quiz of LearnCpp.com. I'm stuck and have a question. Can you add variables inside a std::cout statement? For example:
The program will not display my answer. The program ends as soon as the user presses enter after inputting the values. Thanks for your help ahead of time.
EDIT: Sorry for not posting the entire code. I'm also new to forums. I added the () like someone suggested. When I ran the program I think I saw it display the answer for a split second and it doesn't show that Press any key to continue. . .
#include "stdafx.h"
#include <iostream>
int main()
{
double first_value;
double second_value;
char user_operator;
std::cout << "Enter a double value: ";
std::cin >> first_value;
std::cout << "Enter a second double value: ";
std::cin >> second_value;
std::cout << "Enter one of the following (+, -, *, /): ";
std::cin >> user_operator;
if (user_operator == 43 || user_operator == 45\
|| user_operator == 42 || user_operator == 47)
switch (user_operator)
{
case 43:
std::cout << " " << (first_value + second_value) << "\n";
break;
case 45:
std::cout << " " << (first_value - second_value) << "\n";
break;
case 42:
std::cout << " " << (first_value * second_value) << "\n";
break;
case 47:
std::cout << " " << (first_value / second_value) << "\n";
break;
}
else std::cout << "Please enter a valid operator.";
return 0;
}
Yes, you can perform operations within a chain of std::ostream& operator<<(std::ostream&, T) calls. You just need to obey operator precedence and put parenthesis (()) around the expression to disambiguate in case it's necessary.
Here's a fixed Demo.