Even you answer is correct, it will still indicate it is incorrect .
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string play;
cout << "Do you want to play? ";
cin >> play;
if (play == "yes") {
cout << "Ok, Let's Get Started! \n";
} else {
cout << "Let's play next time!";
}
string test1;
cout << "What does CPU stand for? ";
cin >> test1;
if (test1 == "central processing unit") {
cout << "Correct!\n";
} else {
cout << "Incorrect!\n";
}
return 0;
}
I try to redo/recode the if else statement but it didn't run. I'm expecting that there some expert can help me.
This statement
cin >> test1;
reads a single word, so it can never read the three words of 'central processing unit'. If you want to read lines of text then use getline
getline(cin, play);
and
getline(cin, test1);
Related
I am trying to write a program that counts how many times a specific number is inputted by a user from a while loop. Here is the idea:
#include <iostream>
#include <ctime>
using namespace std;
int main() {
bool progLoop = true;
int Number;
char response;
while (progLoop == true)
{
cout << "Please enter a number: " << endl; \
cin >> Number;
cout << "Number collected!" << endl;
cout << "Number of times that number has been entered: " << endl; //IDK how to do this part which is what I'm looking for.
cout << "Do you want to enter another number?" << endl;
cin >> response;
if (response == 'y')
{
progLoop == true;
}
else if(response == 'n')
{
progLoop == false;
}
}
}
I'm looking for a way that I can store a sort of value of how many times that specific number has been entered into the program. Would like to clarify if there are any questions! Thanks! (Modifying my code would be great!)
Use a hashmap:
#include <unordered_map>
#include <iostream>
#include <ctime>
using namespace std;
int main() {
int number;
char response = 'y';
unordered_map<int, int> m; // your hashmap
while (response == 'y')
{
cout << "Please enter a number: " << endl;
cin >> number;
cout << "Number collected!" << endl;
cout << "Number of times that number has been entered: " << ++m[number] << endl; // preincrement your hashmap at key number
cout << "Do you want to enter another number? (y/n)" << endl;
cin >> response;
response = tolower(response); // give a chance to exit the loop if your user like to use caplock (like Trump)
}
}
Expanding my comment into an answer
One way of doing this is to use an std::vector and every time a new number is inputted, it is pushed back into the vector. Then you can std::count that number when you need to check. In your program, it would look something like this:
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
int main() {
bool progLoop = true;
int Number;
std::vector<int> numCount;
char response;
while (progLoop == true)
{
cout << "Please enter a number: " << endl;
cin >> Number;
numCount.push_back(Number);
cout << "Number collected!" << endl;
cout << "Number of times that number has been entered: " << std::count(numCount.begin(), numCount.end(), Number) << endl;
cout << "Do you want to enter another number?" << endl;
cin >> response;
if (response == 'y')
{
progLoop = true; // You accidentally used == here
}
else if(response == 'n')
{
progLoop = false; // You accidentally used == here
}
}
}
I'm practicing ifstream usage. I want the user to enter the file they want to read, in this example num1.txt specifically. I want the console to read one letter from num1.txt and output it on its own line.
I've ran the code below, and after entering "num1.txt" into the console, I get nothing back. I've tried moving around cout << num << endl; to the inner do statement, but it ends up repeating the number 10 an infinite amount.
What am I doing wrong here?
Contents in num1.txt:
2 4 6 8 10
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string fileName, cont;
ifstream inputFile;
do {
int num = 0;
int total = 0;
cout << "Please enter the file name: ";
cin >> fileName;
inputFile.open(fileName);
if (inputFile.is_open()) {
do {
inputFile >> num;
total += num;
}
while(num > 0);
if (total != 0) {
cout << num << endl;
cout << "Total is: " << total << endl;
}
}
else {
cout << "Failed to open file." << endl;
}
inputFile.close();
cout << "Do you want to continue processing files? (yes or no): " << endl;
cin >> cont;
}
while (cont == "yes");
}
Your inner do loop is not correctly validating that operator>> is actually successful before using num. It should be looking at the stream's error state after each read. The easiest way to do that is to change your do loop into a while loop that uses the result of the read as its loop condition, eg:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string fileName, cont;
ifstream inputFile;
do {
cout << "Please enter the file name: ";
cin >> fileName;
inputFile.open(fileName);
if (inputFile.is_open()) {
int num = 0;
int total = 0;
while (inputFile >> num) {
total += num;
}
inputFile.close();
cout << "Total is: " << total << endl;
}
else {
cout << "Failed to open file." << endl;
}
cout << "Do you want to continue processing files? (yes or no): " << endl;
}
while ((cin >> cont) && (cont == "yes"));
return 0;
}
i'm making simple program to show "True" if user input 'z' and show "False" if user input anything else.
However, the problem is when user input more than a character, such as when user input 'zz' the output is
True
Input : True
and when user input such as 'zs' which should be wrong, the output is
True
Input : Wrong
Here's my code
#include <iostream>
using namespace std;
int main()
{
char input;
cout << "Check input" << endl;
while(true){
cout << "Input : ";
cin >> input;
if(input=='z'){
cout << "True" << endl;
} else {
cout << "Wrong" << endl;
}
}
return 0;
}
I wonder if there are ways to prevent this without change variable type to string?
I use CodeBlocks 16.04 (MinGW) with GNU GCC Compiler on Windows 10 x64
You cannot do that by reading single chars. The point is that if the user enters e.g. zz he actually did enter those two chars and these are the chars you are getting when you read from cin.
Just read a std::string as suggested and check only the first character of the string. That's just as simple that what you're doing.
So you probably want this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cout << "Check input" << endl;
while (true) {
cout << "Input : ";
cin >> input;
if (input.length() > 0 && input[0] == 'z') {
cout << "True" << endl;
}
else {
cout << "Wrong" << endl;
}
}
return 0;
}
Its definitely possible you just have to check the first character and make sure It is the only character entered than flush the buffer to get rid of the rest of the string.
code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char input;
cout << "Check input" << endl;
while (true) {
cout << "Input : ";
cin >> input;
//Check if the input is z and there is only 1 character inputted
if (cin.rdbuf()->in_avail() == 1 && input == 'z') {
cout << "True" << endl;
}
else {
cout << "Wrong" << endl;
}
//Flush the buffer
cin.clear();
cin.ignore(INT_MAX, '\n');
}
return 0;
}
First off I'd like to say I know that using getline is a better alternative, however, i am curious as to why this code doesn't work as intended: and I can't figure out why
#include <iostream>
#include <string>
using namespace std;
int main() {
while(1) {
int input;
cout << "---> ";
cin >> input;
if(cin.fail()) {
char rd = cin.get();
cout << "failure" << rd << "=" << cin.fail() << " " << endl;
}
}
return 0;
}
Intended: if an integer is entered, continue otherwise cin fails, we pull one char from stdin and essentially output it. Then we keep looping.
The way I see it, eventually cin.get() should clear up bad input; but it never does: it gets stuck in an infintie loop. What?
cin.fail() detects whether the value entered fits the value defined in the variable.
But if cin.fail() is true, it means that
a) the entered value does not fit the variable
b) the varialbe will not be affected
c) the instream is still broken
d) the entered value is still in the buffer and will be used for the next "cin >> variable"statement.
Hence you have to do the following:
a) repair the instream via cin.clear();
b) clear the buffer with cin.ignore(std::numeric_limits::max(),'\n');
#include <iostream>
#include <string>
using namespace std;
int main() {
while(1) {
int input;
cout << "---> ";
cin >> input;
if(cin.fail()) {
char rd = cin.get();
cout << "failure" << rd << "=" << cin.fail() << " " << endl;
cin.clear();
cin.ignore();
}
}
return 0;
}
After taking the input integer, it gets a newline as a character when you press enter. Same as for character input
#include <iostream>
#include <string>
using namespace std;
int main() {
while(1) {
int input;
cout << "---> ";
cin >> input;
getchar();
if(cin.fail()) {
char rd = cin.get();
getchar();
cout << "failure" << rd << "=" << cin.fail() << " " << endl;
}
}
return 0;
}
//review3
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
int number;
int main()
{
cout << "Enter a positive number" << endl;
cin >> number;
while (number < 0)
{
cout << "Enter a positive number" << endl;
}
if (number > 0)
{
cout << "Awesome job!" << endl;
}
return 0;
}
This is my code so far. I started with an else if but if the user entered a negative number the program would simply close. I changed this to a while loop and got stuck in an infinite loop. Before I had an if and else if statement. I need to continue to prompt the user until they enter a positive number in c++.
Your while() loop doesn't continue to prompt for input, that's why you're getting an infinite loop - because number never changes!
You can put the input operation into the while() loop like this:
while (cin >> number && number < 0)
{
cout << "Enter a positive number: " << endl;
}
if (cin)
{
cout << "Awesome job" << endl;
}
Thus, during each iteration of the loop the user will be prompted for input.
We check the state of cin afterwards to make sure that the above loop didn't stop because of invalid input (or no input at all).
#include <iostream>
using namespace std;
int number;
int main()
{
cout << "Enter a positive number" << endl;
cin >> number;
if (number < 0) {
cout << "Enter a positive number" << endl;
}
else {
cout << "Awesome job!" << endl;
}
return 0;
}
You can check if you get number or string here
sure in this case you should get input to string variable. If you want to convert it to integer you can use std::stoi
#include <iostream>
#include <string>
#include <cstring>
int main()
{
while(true)
{
std::cout << "Pleaase enter a positive number." << std::endl;
std::string buf;
int x = -255;
std::cin >> buf;
x = std::atoi(buf.c_str());
if(x > 0)
{
break;
}
}
std::cout << "Awesome Job!" << std::endl;
}
You can also do this snippet . Use #include <ctype.h> or <cctype>
while(1){
cin>>number;
if(number<0 || isalpha(number)) return 0;
cout<<"Awesome Job";
}