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 5 years ago.
Improve this question
hello I am trying to do my college homework but I keep getting an error saying expected a ; but I already have a ; on that line. my error is during the cin answer
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Welcome to the Prison of Elders, Guardian, what is your name ?.";
cin >> name;
cout << "are you ready to face a challenge" << name << "!" << endl;
cin answer;
while (1);
return 0;
cin answer;
should be replaced by
cin >> answer;
More generally, the "expected thing" as compiler message should always be treated with caution. It's just some guess that matches the syntax from the compiler. However, it is always the sign of a syntax error somewhere. (And not necessarily at the line where you see the "expected thing")
To elaborate on why ";" was expected :
cin is an identifier, as well as answer
You wrote identifier identifier;, which is never a correct syntax in C++.
the compilator was confused by seeing two identifiers next to each other. So it suggested this :
cin; answer;
which is syntactically correct and solve the confusion, but complete nonsense semantically in your code.
Related
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
Why can't I assign my new_age(age) to another variable (in this case it will be new)?
I get the following errors:
main.cpp:21:9: error: expected type-specifier before ‘=’ token
21 | new = new_age(age)
| ^
This is my code:
#include <iostream>
using namespace std;
int new_age (int & age)
{
return (age + 100);
}
int main()
{
int age {};
new = new_age(age)
cout << "How old are you "
cin >> age
cout << "In 100 years you will be " << new:
return 0;
}
The word new is a keyword in C++. It has a special meaning and cannot be used as name of a variable.
For a full list of all keywords see here.
As an aside, even if new was allowed as an identifier, you have not declared it. To declare and initialize it directly with the value you need to add the variable's type in front of the identifier:
int not_new = new_age(age);
(Also note the semicolon at the end of the declaration which you forgot or mistyped in a lot of statements as well.)
It also seems that you'd want to call new_age(age) after taking input from the user.
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
C++ beginner here with a functions error, I get the error "redefinition of formal parameter". What does this mean? and how can I fix it.
int getGuessFromUser(int guess)
{
std::cout << "Guess my lucky number between 0 and 10: ";
int guess;
std::cin >> guess;
return guess;
}
You have a parameter int guess and a variable int guess
You don't seem to be using the parameter, so perhaps remove it?
int getGuessFromUser()
{
std::cout << "Guess my lucky number between 0 and 10: ";
int guess;
std::cin >> guess;
return guess;
}
You are getting this error because you have already declared the parameter guess in you getGuessFromUser function. You do not need to declare it again, so you can remove the line int guess
You have a function parameter named guess and also a local variable with the exact same name.
This would be just like declaring two variables with the same name in the same scope (like a function), which is not allowed.
Change one of the names to remove the error.
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
this program is simple :
1)take an input string .
2) convert it to long .
3) print convert result.
expected an output,but nothing found.
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
string ch;
scanf("%s",ch);
long l=stol(ch);
printf("%l",l);
return 0;
}
Here's how it's done with C++ I/O. There's very little reason for using C I/O in a C++ program.
#include <iostream>
#include <string>
int main()
{
std::string input;
std::cin >> input; // take an input string
long lval = stol(str); // convert to long
std::cout << lval << '\n' // print the result
}
Now this stuff would be covered in the first chapter of any C++ book. A good book will greatly increase how quickly and how well you learn 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 5 years ago.
Improve this question
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int b;
printf("hello");
for(b=1;b<=100;++b)
{
if(b%10==1){
cout << "\n";
for(int l=0;l<=100;++l)
cout << "-" ;
cout << endl;
}
printf("|%s|",b);
}
return 0;
}
enter image description here
printf which is placed outside of loop body works fine but the one placed in the loop body of for causes some kind of error while running!! take a look at the picture !
Your b is an int.
You give b where printf() expects a pointer to char and will attempt to dereference the value you give as such.
Since the value you give via b is not a valid pointer to anything, your program has some access problem.
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 9 years ago.
Improve this question
The following Code is not working. I get an error at the command cin >> h. What am I doing wrong?
#include <iostream>
using namespace std;
int main()
{
string h = " ";
cout << "hi" << endl;
cin >> h;
cout << h << endl;
system("pause");
return 0;
}
Random guessing:
You forgot to #include <string>
You forgot to include <string> and C++ punished you for that.
Ah, but every man and his dog should know that by not including <string>, you were using the default >> operator, that has well know issues with strings.
C++'s "leave the progammers free to shoot themselves in the foot" philosophy at its best.
C++ lore tells the unfortunate wandereds should use getline instead of cin >>, but there have been heated debates among scholars on this fine doctrine point.