g++ compiling and while loop [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 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;
}
}

Related

money converter c++ gives bracket errors [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
Here is my code:
#include"std_lib_facilities.h"
int main()
{
cout << "Which currency do you wanna exchange for, $? €? or ¥?:\n";
string currency;
string dollars;
string euros;
string yen;
char $;
char €;
char ¥;
if (cin >> $);
cout << "You have chosen dollars.\n";
cout << "Please enter your amount:\n";
int amount;
cin >> amount;
cout << "Your amount is:" << amount*153 << "PKR" << endl;
keep_window_open();
return 0;
}
I tried making a money converter but my code till dollar wont run so I haven't wrote euro or yen yet. thank you for the response.
First of all, don't use those symbols as variable names, since they're not ASCII characters and could get messed up.
Secondly, if (cin >> $); doesn't do what you think it does. All it does reads something into the $ variable, and moves on regardless of what cin actually read because the if statement is terminated by a semicolon. The code after that is going to get executed, regardless of what cin actually read. Instead, you should do
if (condition) {
thing to execute if condition is true
}
Note the use of curly braces.
Thirdly, this is not how you check if the input string matches the dollar sign. What you should do instead, is read the input into a variable, and then compare that variable to the currency symbols.

c++ variable reassignment gets ignored by code [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
I am not a professional programmer but after getting some experience in easy languages like python or matlab, I need to make a little program in C++. For this I try to read user input until the user inputs something sensible - however, this loop never terminates due to my control variable (test2) never being reassigned, even though the corresponding code block is entered. Let me explain in detail:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "Header.h"
using namespace std;
int test2; //variable to stay 1 until user has entered suitable input
string input2[2]; //storage for input strings
int MinimalTest() {
test2 = 1; //control variable is set one
cout << "Enter a string." << endl;
do {
//we will enter a string at least once, and we exit this loop only when the input is suitable
std::string line; //complicated input part - a simple getline() command led to weird behaviour
std::getline(std::cin, line);
std::stringstream linestream(line);
linestream >> input2[i];
cout << "input: " << input2[i] << " test: " << test2 << "input bool: " << input2[i].empty() << endl; //this is just for troubleshooting
if (input2[i].empty()) {
//if the user entered an empty string, he needs to do it again
cout << "Your string is empty. Please enter a valid string (non-empty)." << endl;
}
else {
//if he entered a valid string, we can continue with the next input
cout << "I am here." << endl; //This is for trouble shooting. This code block is entered and executed since this gets printed.
test2 = 0;// this gets ignored for some reason. Hence, the loop never terminates.
}
}(while (test2 = 1);
}
So the first loop never terminates. Test2 never gets reassigned to 0, even though the else command is executed. This boggles my mind tbh - it is just a simple assignment operator on an int. Possible output looks like this (Note how I still got a second problem: strings with a space inside get cut off. I would appreciate any feedback on that as well, even though I try to trouble shoot one thing at a time and this post is not aimed at this problem):
Output example
Thank you very much for your consideration!
All the best,
A very confused newbie.
Change your while condition to test2 == 1
test2 = 1 is an assignment, setting the value to 1.

Program stops without running the while loop [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 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.

for loop result 0 in 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 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.

How to get rid of this error? [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 6 years ago.
Improve this question
I was assigned to do a simple number converter, I followed all the instructions and they seemed pretty straight forward. This is for a beginner course into C++ and I seem to be missing the mark on this program. I continue to get an error stating that I need an initializer before double inputedNumber or that the variable is not in the scope. I have even compared my code to classmates and did what they did but this error still happens...
Any help would be awesome!
Thanks in advance!
#include <iostream>
using namespace std;
int main()
{
double inputedNumber;
cout << "Please input a decimal to be converted.";
cin >> inputedNumber;
cin >> "Number Converter!! The given number is" >> inputedNumber;
}
cin >> "Number Converter!! The given number is" >> inputedNumber;
does not compile, since cin is only for input not output; use cout instead!
cout << "Number Converter!! The given number is" << inputedNumber;
Also, if you really want to convert a number (to an integer for example) use this:
cout << "Number Converter!! The given number is" << static_cast<int>(inputedNumber);
First Line in main function is missing a semicolon.