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.
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 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.
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
I tried to do a phone Sys and I used a while loop in the main{}. I don't know why it only runs one time, it suppose to run infinite time unless I give it command to stop.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void record(string name, int phoneNum, int count);
// main
int main() {
cout << " Welcome to use the Phone Contact Systerm " << endl;
string name;
int phoneNum;
int count = 0;
string signToStop;
cout << " Please enter name and phone number " << endl;
while ( cin >> name >> phoneNum){
cout << " If you want to start the program, enter start " << endl;
cout << " If you want to quit the program, enter quit " << endl;
cin >> signToStop;
if (signToStop == "start"){
record(name, phoneNum, count);
}
else if ( signToStop == "quit" ){
break;
}
count++;
}
}
// record all name info into Name set and record all phone numbers into PhoneNum set
void record(string name, int phoneNum, int count){
string Name[] = {};
int PhoneNum[] = {};
Name[count] = {name};
PhoneNum[count] = {phoneNum};
// now start to record all the info into .txt document
ofstream phoneFile;
phoneFile.open("contact.txt");
phoneFile << name << " " << phoneNum << endl;
}
The result is:
Welcome to use the Phone Contact Systerm
Please enter name and phone number
Molly 5307659229
Process finished with exit code 0
Maybe try ulong int for the phone number, it might be too long. Also I might add that I am a bit confused, as your function record() has a 3rd argument that has no default argument. Your problem might lie there too. As without a default you need to put the argument in when it is used.
As spectras said, a phone number is not really an integer, and so it's not a "number" in the programming (or even mathematical) sense.
It's more like a sequence of digits; that is, a string.
You have two problems when you try to interpret it as an int:
Your int type is too small for the value (this is what's causing your loop to end)
Leading zeroes are not meaningful (at best, it's used to flip into octal mode, which is not what you wanted).
I'd instead read it as a string. You can still validate it later, like "is every character a digit?".
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 needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I came across a question whereby you are told there will be unspecified number of queries hence you got to keep on taking input for this unspecified number of queries
all i know is that in c++ or even in another programming language, when the program needs to take unspecified number of input, we prompt the user to enter a certain value which will be used to terminate the infinite loop e.g
for (;;)
{
cout<<"enter 0 to stop taking input"<<endl;
int value;
cin>>value;
if (value==0)
{break;}
}
my question is how will i handle the question stating the input will be unspecified and its in an online environment
for this kind of problem you will use a loop control variable and a while loop for user input validation
while loop is use for input validation while for loop is use for specific
number of times only and it is not really suggested for unknown number of times
same with while loop, do while loop can also perform input validation the
only difference is, it will prompt the user first before evaluating the test condition
int myNum; // this is the loop control variable
cout << "enter a number, enter 0 to stop taking input " << endl;
cin >> myNum;
while(myNum!=0)
{
cout << "cograts you did not enter zero digit" << endl;
cin >> myNum;
}
use while (cin >> a) :
to see how if it works create an input file 1.in and write a bunch of numbers in it, then pipe it to your executable fle ./a.out <1.in
#include <iostream>
using namespace std;
int main() {
int a;
while (cin >> a) {
cout << a << "\n";
}
return 0;
}
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.