printf won't PRINT in if statement, specifically the gender variable - if-statement

Below I have some code that asks for the users gender to enter a website, issue is I placed a printf() function to display a message of the user's gender inside of an if statement and now it does not print.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int age;
char gender;
printf("How old are you? \n");
scanf_s(" %d", &age);
printf("What is your gender \n");
scanf_s(" %c", &gender);
if (age >= 18) {
printf("You may enter this website ");
}
if (gender == 'f') {
printf("m'lady");
}
if (gender == 'm') {
printf("dude");
}
if (age < 18){
printf("Nothing to see here!");
}
system("pause");
return 0;
}

Related

Why are my 'if' and 'else if' statements not working?

My question is to both:
Allow the user to enter a name and get custom messages based upon the name entered, and, when you collect the name from the user, convert the collected name to lowercase.
Thus, in my 'void test_user_details()' function, I made a custom message if the user puts in either Tim or John, but when I test the code it doesnt work? It skips the custom message. Also, I used "to_lowercase(name)" but the output isnt lowercase, it stays the same.
It seems like my if and else if functions arnt running at all and I dont know why.
Here is the full code of the program:
#include "splashkit.h"
#include <string>
using namespace std;
string read_string(string prompt)
{
string result;
write(prompt);
result=read_line();
return result;
}
int read_integer(string prompt)
{
string line;
line = read_string(prompt);
return convert_to_integer(line);
}
***************
void test_user_details()
{
string name;
string line;
int age;
name = read_string("What is your name: ");
line = read_string("What is your age: ");
age = convert_to_integer(line);
write_line("Hello, " + name);
if (to_lowercase(name) == "Tim")
{
write_line("\nWelcome Tim, creator of this program");
}
else if (to_lowercase(name) == "John")
{
write_line("\nWelcome John, tester of this program");
}
else
{
write_line("Are you Tim or John?");
}
write("Age: ");
write_line(age);
}
void play_game()
{
string line;
int guess;
int target;
target = rnd(100) + 1;
write_line("Guess a number between 1 and 100 (inclusive)");
while(guess != target)
{
write("Enter guess: ");
line = read_line();
guess = convert_to_integer(line);
if (guess < target)
{
write_line("Sorry, the number is greater than " +
to_string(guess));
}
else if (guess > target)
{
write_line("No, the number is less than " +
to_string(guess));
}
}
write_line("You guessed correctly! The number was " +
to_string(target));
}
int main()
{
string line;
int option;
do
{
write_line("1. Play Game");
write_line("2. Quit");
write("Choosen option: ");
line = read_line();
option = convert_to_integer(line);
switch(option) //switch function
{
case 1:
test_user_details();
play_game();
break;
case 2:
write_line("Game ends.");
break;
default:
write_line("Please enter an option from the menu");
}
} while (option!= 2);
return 0;
}
assuming your to_lowercase returns a string in lower case it will never match:
"Tim" or "John"

Char gets garbage value when reading data in loop

I have a problem with reading data in loop. Although the code seems to me to be good, for some test cases it breaks down. Here is simplified program code.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
string str1, str2, str3;
char choice;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> choice;
cin.ignore();
getline(cin, str1);
if (choice == 'a') {
getline(cin, str2);
getline(cin, str3);
// ...
// operations not related to data input
// ...
} else if (choice == 'b') {
getline(cin, str2);
// ...
// operations not related to data input
// ...
} else if (choice == 'c') {
// ...
// operations not related to data input
// ...
}
}
return 0;
}
The input value of choice is always 'a', 'b' or 'c', but during the program it sometimes gets other value.
I tried to use scanf instead, but it didn't change the result.
Does anyone have an idea how to solve this problem?

it shows no match for operator&&

#include < cstdio>
#include < cstdlib>
#include < iostream>
#include < string>
using namespace std;
int main ( int nNumberofArgs, char* pszArgs[])
{
string name;
string password;
int ans1;
printf("Turn on the BEDrive??\n(write an ODD number for 'no' and EVEN 'yes'\n");
cin >> ans1;
while(ans1%2==0)
{
printf("Hello Master... Welcome to the BEDrive System...\nwould you like to login...\n);
cin.ignore(10, '\n');
cin.get();
printf("Enter your login ID: ");
cin >> name;
printf("Enter your password: ");
cin >> password;
if(name = "smartyguy1" && password = "Bhanuhanu1")
{
printf("WELCOME MASTER!!!!.\nDo you want to change your password(y/n)?");
}
else
{
printf("Login FAILED!!!:( ");
}
return 0;
}
}
when I compile this code, it shows error: no match for 'operator&&' (operand types are 'const char [11]' and 'std::__cxx11::string {aka std::__cxx11::basic_string}') plz help me with this
You had some errors in your code. I fixed it for you.
You missed an ending " on line 30.
#include <cstdio> // "< cstdio>" is not correct, no space is allowed here
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main ( int nNumberofArgs, char* pszArgs[])
{
string name;
string password;
int ans1;
printf("Turn on the BEDrive??\n(write an ODD number for 'no' and EVEN 'yes'\n");
cin >> ans1;
while(ans1%2==0)
{
printf("Hello Master... Welcome to the BEDrive System...\nwould you like to login...\n"); // you missed ending " here
cin.ignore(10, '\n');
cin.get();
printf("Enter your login ID: ");
cin >> name;
printf("Enter your password: ");
cin >> password;
if(name == "smartyguy1" && password == "Bhanuhanu1")
{
printf("WELCOME MASTER!!!!.\nDo you want to change your password(y/n)?");
}
else
{
printf("Login FAILED!!!:( ");
}
return 0;
}
}

no match for operator == or != in my code within the do while loop

within the DO-WHILE loop the == and != operators have "no match"
#include <iostream>
using namespace std;
int main()
{
string password = "gang";
int input;
cin >> input;
do{
cout<<"Enter password here: "<<endl;
if(input == password){
break;
}
else{
cout<<"Password incorrect"<<endl;
}
}
while(input != password);
cout<<"Password correct"<<endl;
return 0;
}
Error:
C:\Users\New User\Desktop\c++\666666666666658uttu\main.cpp|18|error: no match for 'operator==' (operand types are 'int' and 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}')|
Because you are comparing a string with a int
You're reading the input in as an int, but comparing it to a string.
Do this
#include <iostream>
using namespace std;
int main()
{
string password = "gang";
string input; // <<<<<<HERE
cin >> input;
do{
cout<<"Enter password here: "<<endl;
if(input == password){
break;
}
else{
cout<<"Password incorrect"<<endl;
}
}
while(input != password);
cout<<"Password correct"<<endl;
return 0;
}
The program in any case is wrong.:)
The variable input is declared as having the type int.
int input;
Then you are trying to compare the entered integer with an object having the type std::string.
string password = "gang";
// ...
if(input == password){
Moreover the loop is infinite because the variable input even if it was declared as having the type std::string is not changed within the loop.
Also you should include header <string>.
The program can look the following way
#include <iostream>
#include <string>
int main()
{
std::string password = "gang";
std::string input;
do
{
std::cout << "Enter password here: ";
if ( not ( std::cin >> input ) or ( input != password ) )
{
std::cin.clear();
std::cout << "Password incorrect\n";
}
} while ( input != password );
std::cout << "Password correct" << std::endl;
return 0;
}

Arrays and Searching Them

I am trying to finish my final debugging tonight. My problem is that I have been writing this code for a couple days and it has a few problems. Previous Post
It now compiles and does not crash, but there are a few issues with my functions not working properly (or at all).
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string bookTitle [50];
string bookAuthor [50];
int loadData (string pathname);
int showall (int counter);
int authorSearch (string bookAuthor [50]);
int main ()
{
string pathname;
int counter=0;
char choice;
cout<<"Input the name of the file to be accessed: ";
cin>>pathname;
loadData (pathname);
showall (counter);
cout<<"\n\n\n\n What would you like to do \n (A for Author Search , T for Title Search, Q to quit):";
cin>>choice;
while (choice != 'Q' , choice != 'q')
{
if (choice == 'A', choice == 'a')
{
int authorSearch (string bookAuthor [50], char choice);
}
if (choice == 'T', choice == 't')
{
int titleSearch (string bookTitle [50], char choice);
}
}
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
}
int loadData (string pathname) // Loads data from infile into arrays
{
fstream infile;
int counter = 0;
infile.open(pathname.c_str()); //Opens file from user input in main
if( infile.fail() )
{
cout << "File failed to open";
return 0;
}
while (!infile.eof())
{
infile >> bookTitle [counter] ; //takes input and puts into parallel arrays
infile >> bookAuthor [counter];
counter++;
}
infile.close();
}
int showall (int counter) // shows input in title(author) format
{
cout<<bookTitle<<"("<<bookAuthor<<")";
}
void authorSearch (string bookAuthor [50], char choice) // Function to search Author Array
{
string target = "";
cout<<"Which author would you like to search for: "<<target; //input
for (int count = 0; count++;)
{
if(bookAuthor[count] == target) //tests input against array and outputs result
{
cout<<bookTitle[count]<<bookAuthor[count];
}
}
}
void titleSearch (string bookTitle [50], char choice) // Function to Serch Title Array
{
string target = "";
cout<<"Which author would you like to search for: "<<target; //input
for (int count = 0; count++;)
{
if(bookAuthor[count] == target) //tests input against array and outputs result
{
cout<<bookTitle[count]<<bookAuthor[count];
}
}
}
Latest Version , no major improvements. I am having trouble getting the functions to work after the menu selection. ShowAll seems to work but outputs hex. Thanks again everyone!
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string bookTitle [50];
string bookAuthor [50];
int loadData (string pathname);
int showall (int counter);
void authorSearch (string bookAuthor [50]);
void titleSearch (string bookTitle [50]);
int main ()
{
string pathname;
int counter=0;
char choice;
cout<<"Input the name of the file to be accessed: ";
cin>>pathname;
loadData (pathname);
showall (counter);
cout<<"\n\n\n\n What would you like to do \n (A for Author Search , T for Title Search, Q to quit):";
cin>>choice;
while (choice != 'Q'|| choice != 'q')
{
if (choice == 'A'|| choice == 'a')
{
void authorSearch (string bookAuthor [50], char choice);
}
if (choice == 'T'|| choice == 't')
{
void titleSearch (string bookTitle [50], char choice);
}
}
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
}
int loadData (string pathname) // Loads data from infile into arrays
{
fstream infile;
int counter = 0;
infile.open(pathname.c_str()); //Opens file from user input in main
if( infile.fail() )
{
cout << "File failed to open";
return 0;
}
while (!infile.eof())
{
infile >> bookTitle [counter] ; //takes input and puts into parallel arrays
infile >> bookAuthor [counter];
counter++;
}
infile.close();
}
int showall (int counter) // shows input in title(author) format
{
cout<<bookTitle<<"("<<bookAuthor<<")";
}
void authorSearch (string bookAuthor [50], char choice) // Function to search Author Array
{
string target = "";
cout<<"Which author would you like to search for: "<<target; //input
for (int count = 0; count++;)
{
if(bookAuthor[count] == target)
{
cout<<bookTitle[count]<<bookAuthor[count];
}
}
}
void titleSearch (string bookTitle [50], char choice) // Function to Serch Title Array
{
string target = "";
cout<<"Which title would you like to search for: "<<target; //input
for (int count = 0; count++;)
{
if(bookAuthor[count] == target) //tests input against array and outputs reults
{
cout<<bookTitle[count]<<bookAuthor[count];
}
}
}
The comma operator should be replaced with logical and or or, && and || respectively. See uses of the comma operator. Also, authorSearch is a void function. If you want to call authorSearch, simple write authorSearch(...) instead of int authorSearch(...).
Additionally, you need to make sure your prototypes are consistent with your implementations. int authorSearch (string bookAuthor [50]) is not the same as void authorSearch (string bookAuthor [50], char choice). You've mismatched their types and their parameters.
1) The showall() function outputs hex because you can't display arrays that way, you need some kind of loop. It's just printing the starting address of each array.
2) In your search functions you never read the target string from the user.
3) These for() loops will never execute:
for (int count = 0; count++;)
{
...
}
You set count to 0 and then test the value before incrementing. The test fails and the loop body isn't executed.
4) Take care when you fix the for() loops. I don't see any tests to prevent using an invalid index past the (hardcoded) size of your arrays.