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.
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 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
Im just starting to learn C++. While writing a simple calculator, I found that when calling a void function, it doesn't print when it should. I have simplified the code to better represent my problem.
#include <iostream>
using namespace std;
void helloguys()
{
cout << "test";
}
int main()
{
cout << "This is a ";
void helloguys();
cout << " guys.";
}
I expected to get "This is a test guys.", but all I got is "This is a guys."
The compiler never reported any kind of problems.
Try removing void before your function call:
int main()
{
cout << "This is a ";
helloguys();
cout << " guys.";
}
helloguys(); will call the function.
void helloguys(); is a function prototype, not a call.
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 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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm learning to use C++ and I decided to create a password program where the user is asked for the password and it compares the user input to the password and returns a wrong or a right. For some reason, this programs always returns a wrong and I'm not sure why. It must be something to do with comparing the strings but I'm not sure.
#include <iostream>
#include <string>
using namespace std;
int main(){
string pass = "password";
string input;
cout << "What is your password: ";
cin >> input;
if (input==pass){
cout << "Correct" << endl;
}else{
cout << "Wrong" << endl;
}
return 0;
}
I would love some help from programmers who are in any way more well versed in C++ as I've just transferred over to C++ from Python and the transitions a bit rocky.
1.you could use compare function, to see:http://www.cplusplus.com/reference/string/string/compare/
2.you should debug at line if (input==pass){
to print pass and input and check if they are the same.
I found I needed to:
#include <string>
to get the definition of the insertion operator (for cin >> input;) and std::string::operator==() (for if (input==pass)). Once I did that, it worked fine in Visual C++.
What compiler are you using?