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 4 years ago.
Improve this question
My while loop is not working. The code runs correctly on the Codecademy website compiler. I then compile it with Visual Studio, run it from the Command prompt and input a number. The program stops prematurely even if the number is the correct one.
#include <cstdlib>
#include <iostream>
int main() {
int answer = 8;
int guess;
int tries;
std::cout << "I have a number between 1-10.\n";
std::cout << "Please guess it: ";
std::cin >> guess;
while (guess != 8 && tries < 50) {
std::cout << "Wrong guess, try again: ";
std::cin >> guess;
tries++;
}
if (guess == 8) {
std::cout << "You got it!\n";
}
}
As #rsjaffe and #Ken White have said in the comments, the tries variable is unitiailized, meaning that the location in memory that the variable is pointing to is "junk" (left over memory). Try to give it an initial value, like this:
int tries = 0;
which will instantiate and initialize the tries variable.
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 11 months ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
My "num = -num" line inside of my "if (num<0)" line still affects results, even if my input is greater than 0.
#include <iostream>
int main()
{
std::cout << "Enter a positive number: ";
int num{};
std::cin >> num;
if (num < 0)
std::cout << "Negative number entered. Making positive.\n";
num = -num;
std::cout << "You entered: " << num;
return 0;
}
To have multiple statements inside an if, you must use brackets.
And at this point of learning the language, I would recommend just always using brackets.
if (num < 0) {
std::cout << "Negative number entered. Making positive.\n";
num = -num;
}
Unlike languages like python, leading whitespace is not meaningful to the compiler in 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 2 years ago.
Improve this question
ok so i writ this little code in c++. keep in mind i just started learning c++ so this problem confused me a lot
when i run this code in code blocks (
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer = 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)
the answer value is always set to one even if i type 0
i tried coding another if statement for the answer value if it was 0 but that didn't work either
In C++, = operator is an assignment operator and it sets value of left operand to value of right operand. Therefore the value of Answer becomes 1 thanks to Answer = 1.
You should use == operator to check equality.
Use the == operator in your code, as = operators does the job of assigning values.
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer == 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)
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
I am a newbie in C++, I started to learn coding in C++ two weeks ago. Why does my code below always give me result 0 when I build and run? Please help
# include <iostream>
# include <string>
using namespace std;
int main ()
{
int input = 1;
cout << "input your number : \n";
cin >> input;
int faktorial = 1;
for(int i=1;i<=input;i++)
{
faktorial = faktorial * i;
}
cout << "factorial value from number " << input << " is " << faktorial << endl;
}
Your code works: https://ideone.com/CYFaxo
I suspect your problem is, you are looking at program exit code. When you don't return any value from main, program exit code is 0 (this is special case, and only non-void function where you may leave the return statement out), which conventionally means success (non-zero exit code usually indicates some kind of error, by convention).
Try to find the program output from your IDE, it should have the correct printout.
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
Prerequisites: Atom as code editor with gpp-compiler(plugin), that uses g++ to compile and run cpp files in the editor.
Some not real example code to understand the problem:
int main()
{
int number;
while(cin >> number)
{
cout << "Your number is " << number << endl;
}
}
So this program can easily compile by g++ compiler, the problems appears at runtime, when compiled program starts in terminal and... It's just don't work. There is no something else but "Press any key to continue..." There is no even mistakes.
So the compiler cannot support this loop argument? (while(cin>>number))
And yes, gpp-compiler in Atom works fine with other types of scripts.
Sorry, if this question is stupid but i just want to know why this happens. Thank you!
Some edits:
So yeah. I can't competently explain my problem. So my problem is not the while loop argument, i just don't understand why program starts in empty terminal(with message above), while on my phone it also compiles by g++ and the program works perfectly .-.
The (cin >> number) condition always evaluates to true until you send an EOF character to it. On Windows it is Ctrl + Z. The reason you are not seeing anything on standard output is that the program waits for your to enter a value and press Enter. Afterwards it enters the endless loop. Modify your program to include some simple logic:
#include <iostream>
int main() {
char choice = 'y';
int number;
while (std::cin && choice == 'y') {
std::cout << "Enter the number: ";
std::cin >> number;
std::cout << "Your number is " << number << std::endl;
std::cout << "Repeat? y / n: ";
std::cin >> choice;
}
}
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
thanks for taking the time to help me out.
I'm really new with C++ and Xcode. I was working on a simple program to help me understand loops, so my goal was to make a simple "echo machine". This is my code:
string words;
int main()
{
do {
cout << "Enter text.";
cin >> words;
cout << "You entetered " << words << "!";
}
while (words != "goodbye");
return 0;
}
My result is nothing but lldb in parenthesis. I am very frustrated and can't find what I'm doing wrong anywhere. Please help and thank you so much.
Are you just missing the include directives for the standard headers you're using?
Try this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string words;
do {
cout << "Enter text: ";
cin >> words;
cout << "You entered " << words << "!\n";
} while (words != "goodbye");
return 0;
}