I did my "Hello World", I'm just getting started on my programming adventure with C . Here is the first thing I've written, what are some ways to get it to end with user input? I'd like a yes or no option that would terminate the program. Also any feedback is welcome, thank you
#include <iostream>
using namespace std;
void Welcome();
void calculateNum();
void tryAgain();
int main()
{
Welcome();
while (true)
{
calculateNum();
tryAgain();
}
system("pause");
}
void calculateNum()
{
float userNumber;
cin >> userNumber;
for (int i = 100; i >= 1; i--)
{
float cNumber = i* userNumber;
cout << i << " >>>>> " << cNumber << endl;
}
}
void Welcome()
{
cout << "Welcome \n Enter a number to see the first 100 multiples \n";
}
void tryAgain()
{
cout << "Try again? Enter another number... ";
}
CodePudding user response:
Here is one option:
- Switch to
do ... while
loop, with the condition at the end. - Make your
tryAgain()
function return a boolean and put it in thewhile
condition. - In
tryAgain
function read input from the user, and compare it to expected answers.
First, lets add a new header for string
, it will make some things easier:
#include <string>
Second, lets rebuild the loop:
do {
calculateNum();
} while (tryAgain());
And finally, lets modify the function:
bool tryAgain() {
string answer;
cout << "Try again? (yes / no)\n";
cin >> answer;
if (answer == "yes") return true;
return false;
}
Now, there is a slightly shorter way to write that return, but it might be confusing for new learners:
return answer == "yes";
You don't need the if
because ==
is an operator that returns bool
type value.
CodePudding user response:
You can change your calculateNum() in the following way:
- Change the return value of your calculateNum() function into bool to indicate whether the program shall continue or stop
- read the input into a std::string
- check if the string is equal to your exit string like 'q' for quit 3.a in that case, your function returns false to indicate the caller that the program shall stop 3.b otherwise, create a stringstream with your string and read the content of the stream into your float variable and continue as you do like now
- In your loop in your main function you break if calculateNum() returned false
Here is a simple solution:
#include <iostream>
// Here are two new Includes!
#include <sstream>
#include <string>
using namespace std;
void Welcome();
// Change return value of calculateNum()
bool calculateNum();
void tryAgain();
int main()
{
Welcome();
while (true)
{
if (!calculateNum())
break;
tryAgain();
}
system("pause");
}
bool calculateNum()
{
//Read input into string
string userInput;
cin >> userInput;
//Check for quit - string - here just simple q
if (userInput == "q")
return false;
//otherwise use a std::stringstream to read the string into a float as done before from cin.
float userNumber;
stringstream ss(userInput);
ss >> userNumber;
//and proces your numbers as before
for (int i = 100; i >= 1; i--)
{
float cNumber = i * userNumber;
cout << i << " >>>>> " << cNumber << endl;
}
return true;
}
void Welcome()
{
cout << "Welcome \n Enter a number to see the first 100 multiples \n";
}
void tryAgain()
{
cout << "Try again? Enter another number... ";
}
Having your users input in a string you can even do further checks like checking if the user entered a valid number, interpret localized numbers like . and , for decimal delimitters depending on your system settings and so on.