C++ If-else statement using strings and comparing strings - c++

when I enter (Im qwerty) as (y), the program shows "Your account has been deactivated" instead of "Your password is incorrect". I've searched for same problems but const. char and using strcmp is too complicated for me and my instructor does not use that kinds of codes.I'm very eager to know what must I do to make my program right. (Tnx in advance)
#include <iostream>
using namespace std;
int main () {
string y;
cout << "Enter Icode: ";
cin >> y;
if (y == "Im qwerty")
cout << "Your password is incorrect.";
else
cout << "Your account has been deactivated.";
cin.get();
return 0;
}

The problem is that cin >> y; reads one word, whereas there are two words in "Im qwerty". In other words, this program always outputs "Your account has been deactivated." because one word never matches two words.
If you would like to read multiple words, the easiest is to read an entire line, e.g. replace cin >> y; with getline(cin, y);.

Related

loop involving "cin.fail()" running multiple times

I am having trouble with the following code. It is meant to keep asking for a valid input until an integer or double is inputted. It works as intended for characters, however when I input a string of length more than 1, it will run the loop multiple times. For example, the input "hello" with cause "Please enter a valid number" to be printed 5 times. Interestingly "h llo" will only print the sentence 4 times.
int gamenumber;
while(true)
{
cin >> gamenumber;
if(cin.fail())
{
cout << "Please enter a valid number" << endl;
cin.clear();
cin.ignore();
} else
break;
I did manage to fix this issue by replacing "cin.ignore()" with "cin.ignore(1000, '\n')".
But regardless, it's bugging me that I don't understand why "cin.ignore()" alone doesn't fix this? Is there a way to fix the above code without using "cin.ignore(1000, '\n')"? (This is part of a homework assignment, and we may not be allowed to use "cin.ignore(1000, '\n')")
Thank you!
You need use ignore with the overloaded one, see this anser here.
Or you can just need to run getline to drain the contents, but this way is slower and unnecessary.
#include <iostream>
#include <string>
int main()
{
double n;
while( std::cout << "Please, enter a number\n"
&& ! (std::cin >> n) )
{
std::cin.clear();
std::string line;
std::getline(std::cin, line);
std::cout << "I am sorry, but '" << line << "' is not a number\n";
}
std::cout << "Thank you for entering the number " << n << '\n';
}

Program is not asking 3 questions and uses my answer to the first question for the other two

I am trying to ask the user to enter the names of 3 of their friends, however, it only asks one question and writes the answer from my first one in the second and third.
#include <iostream>
using namespace std;
int main()
{
char first_name;
cout << "Please enter a name: ";
cin >> first_name;
cout << first_name << endl;
char second_name;
cout << "Please enter a name: ";
cin >> second_name;
cout << second_name << endl;
char third_name;
cout << "Please enter a name: ";
cin >> third_name;
cout << third_name << endl;
return 0;
}
You should probably be using string in your code to take the input of names. In names you are probably passing more than one character. The first one is read by first_name and any further character will be read by the following character, specifically cin>>second_name and cin>>third_name would read the 2nd and 3rd character of your input.
char a;
char b;
cin>>a; //will only read one character and stop
cin>>b; //will read the second character of the input...
//be that after pressing enter(a Enter b) or continuous input (ab)
cout<<a<<" "<<b; //will output 1st and 2nd character only
This will happen even if you don't press the Enter key explicitly and this is why your program uses the answer of first question(which is probably more than 1 character since it is a name) in your code as the answer to 2nd and 3rd questions as well.
So for your purpose, you are better of using string to take input from the users.
Hope this clears your doubt !
You tried to hold a lot of chars (one word) in one char who can hold only one char.
#include <iostream>
#include <string> // We need a string, container to hold a chars. Something like array of chars but have a few difference.
using namespace std; // You should avoid using this but in that short code this doesn't matter
int main()
{
// You don't need separate container for this code
// Then we create one container to holds all of inputs
string input;
cout << "Please enter a name: ";
cin >> input; // Put input from user in our input(string)
cout << input << endl; // Print input
// Next code is the same as above
cout << "Please enter a name: ";
cin >> input;
cout << input << endl;
cout << "Please enter a name: ";
cin >> input;
cout << input << endl;
return 0;
}
I special avoided a few elements like using function because this must be simple as possible.

cin skipping variables after entering name

I get an error when I try to run this. It lets me input the employee's name, and then once I press Enter it skips to the end, flying by hours and pay rate without letting me input them.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string EmployeeName; //Employee Name
float EmployeeHours;
float EmployeePayRate; //Emplyee Hours, Employee PayRate
cout << setw(50) << "Employee Name: ";
cin >> EmployeeName;
cout << setw(50) << "EmployeeHours: ";
cin >> EmployeeHours;
cout << setw(50) << "EmployeePayRate: ";
cin >> EmployeePayRate;
system("PAUSE");
return 0;
}
I think the comment by #AleksandarStojadinovic provides the clue to your problem. Use
std::getline(std::cin, EmployeeName);
to read the name. Rest can be as they are.
I am having an error when I try to run this. It lets me input the
employees name, and then once you press enter it skips to the end.
flies by hours and pay rate without letting me input that.
Your name must be comprised of two/three names, maybe first middle and last or something like that. cin deals space separated string and enter separated string in the same way i.e it will take each space separated string as different input. And for that if your name has two names, the first one goes right but the second one goes to a variable which is declared as float and the program terminates abnormally. That's why using cin in this case isn't right at all.
You need to use another way to avoid that, like using getline(cin,EmployeeName) .

How to get two inputs from a same input (C++)

Title probably sounds confusing so first I'll show you my code, I made this simple program to get two input values and multiply them, and another thing, but that's not important, It works correctly:
#include <iostream>
using namespace std;
main()
{
int a,b,c,d,e;
char j = 4;
cout << "Welcome to Momentum Calculator\n\n";
cout << "------------------------------\n";
cout << "Please Enter Mass in KG (if the mass in in grams, put \"9999\" and hit enter): \n\n";
cin >> a;
if (a==9999) {
cout << "\nPlease Enter Mass in grams: \n\n";
cin >> d;
}
else {
d = 0;
}
cout << "\nPlease Enter Velocity \n\n";
cin >> e;
if (d == 0)
{
c = (a*e);
}
else {
c = (e*d)/100;
}
cout << "\nMomentum = " << c;
cin.get();
cin.ignore();
while (j == 4)
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
main();
}
}
Now as you can see, my variable is an int (integer) and my problem is If I enter an English letter (a-z) or anything that is not a number will cause it to repeat my program unlimited times at an unlimited speed. I want a string/char to see if my var "a" is a letter or anything but don't know how to. I can do it, however, I want user to input only one time in "a" and mine makes him to enter again. Please Help :)
There is a function called isalpha in ctype library, checks whether your variable is an alphabetic letter so you can do using isalpha function.
Will isdigit or isalpha from standard library help you?
P.S.
1KG contains 1000 grams, so you should divide by 1000, not by 100;
UPDATE:
Seems I understood your question...
You need cin.clear(); before cin.get() and cin.ignore().
Otherwise the these calls won't do anything, as cin is in an error state.
I think you can get a as an String, and see if it contains English letter or not, if it contains, again ask for the input ( you can do it in a while loop ). And when a correct input entered, parse it and find what is it's number.

How to make the user type a value instead of storing a constant value?

I have made a simple program in C++ and this is the code:
#include <iostream>
using namespace std;
int main()
{
int number;
int square;
number = 5;
square = number * number;
cout << "The square is ";
cout << square;
return 0;
}
what it does is basically taking the integer "5" and get the square value on the screen and so on...
my question is:
how can I make the program take any value from the user instead of storing a value in the memory?
than Q.
Your code makes use of cout to print. C++ makes cin available for input from the console:
int x;
cin >> x;
"An example is worth a thousand words..."
Well cout takes some var. from memory and prints it out on the screen, right?
Well, cin does the exact opposite, it takes in some value from the keyboard and puts it in your memory..
You have to take in the value with the help of cin command, like this:
int a; //lets say you have a variable
cout << "Enter a value here: "; //prompts the user to enter some number
cin >> a; //this line will allow the user to enter some value with the keyboard into this var.
int square = a * a;
cout << "The square is: " << square;
Hope it helps...
Just replace:
number = 5;
with:
cout << "What's the number? ";
cin >> number;
You already know how to use cout to generate output, this simply uses cin to retrieve input.
Keep in mind that, while this may be okay for small test programs or learning, data input in real programs tends to be a little more robust (such as if you enter the string xyzzy when it's trying to input an int variable).