char iCharSelect()
{
//CHARACTER CHOICE
cout
<< "\n\n\n Choose Your Stoner:\n\n"
<< "\n 1 = Chris\n"
<< "\n 2 = James\n"
<< "\n 3 = Hunter\n"
<< "\n 4 = Antonio (Alt. Route)\n"
<< "\n\n Enter Character Number: ";
int iChar = INITIALIZED;
cin >> iChar;
//CHARACTER NAME STRING
string sCharName;
//CHARACTER CHECK
if (iChar == 1){sCharName = "Chris";}
else if (iChar == 2){sCharName = "James";}
else if (iChar == 3){sCharName = "Hunter";}
else if (iChar == 4){sCharName = "Antonio";}
else {
cout << "\n\n\n\tYOU GET NOTHING!!";
system("PAUSE>NUL");
//PROGRAM CLOSES HERE-------------------------------------
cout << iChar;
system("PAUSE>NUL");
}
char *chCharName = &sCharName[0];
return *chCharName; }
What am I doing wrong? The program closes at the marked point in the code and trying to print iChar yields nothing..?
I think you should do:
if (ichar == '1')
same for 2,3..
The way you do it, is ask if the char inserted hascii is equal to 1/2.. It does not equal, so you get to the last "if" statement.
You are returning a pointer return *chCharName; but you've labeled the function as char. One of your problems is that you need to label the function to return char*
char* iCharSelect()
The main issue with your code is that you do not set chCharName if none of those conditions in the if() statement are true. If this is the case, you will attempt to return the first character of an empty string, which leads to undefined behavior.
In addition, the end of the function uses pointers when there is no need to use pointers.
A reasonable implementation would look like this:
if ( sCharName.empty())
return '\0';
return sCharName[0];
Related
Please note that I am a complete beginner at C++. I'm trying to write a simple program for an ATM and I have to account for all errors. User may use only integers for input so I need to check if input value is indeed an integer, and my program (this one is shortened) works for the most part.
The problem arises when I try to input a string value instead of an integer while choosing an operation. It works with invalid value integers, but with strings it creates an infinite loop until it eventually stops (unless I add system("cls"), then it doesn't even stop), when it should output the same result as it does for invalid integers:
Invalid choice of operation.
Please select an operation:
1 - Balance inquiry
7 - Return card
Enter your choice and press return:
Here is my code:
#include <iostream>
#include <string>
using namespace std;
bool isNumber(string s) //function to determine if input value is int
{
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;
return true;
}
int ReturnCard() //function to determine whether to continue running or end program
{
string rtrn;
cout << "\nDo you wish to continue? \n1 - Yes \n2 - No, return card" << endl;
cin >> rtrn;
if (rtrn == "1" and isNumber(rtrn)) { return false; }
else if (rtrn == "2" and isNumber(rtrn)) { return true; }
else {cout << "Invalid choice." << endl; ReturnCard(); };
return 0;
}
int menu() //function for operation choice and execution
{
int choice;
do
{
cout << "\nPlease select an operation:\n" << endl
<< " 1 - Balance inquiry\n"
<< " 7 - Return card\n"
<< "\nEnter your choice and press return: ";
int balance = 512;
cin >> choice;
if (choice == 1 and isNumber(to_string(choice))) { cout << "Your balance is $" << balance; "\n\n"; }
else if (choice == 7 and isNumber(to_string(choice))) { cout << "Please wait...\nHave a good day." << endl; return 0; }
else { cout << "Invalid choice of operation."; menu(); }
} while (ReturnCard()==false);
cout << "Please wait...\nHave a good day." << endl;
return 0;
}
int main()
{
string choice;
cout << "Insert debit card to get started." << endl;
menu();
return 0;
}
I've tried every possible solution I know, but nothing seems to work.
***There is a different bug, which is that when I get to the "Do you wish to continue?" part and input any invalid value and follow it up with 2 (which is supposed to end the program) after it asks again, it outputs the result for 1 (continue running - menu etc.). I have already emailed my teacher about this and this is not my main question, but I would appreciate any help.
Thank you!
There are a few things mixed up in your code. Always try to compile your code with maximum warnings turned on, e.g., for GCC add at least the -Wall flag.
Then your compiler would warn you of some of the mistakes you made.
First, it seems like you are confusing string choice and int choice. Two different variables in different scopes. The string one is unused and completely redundant. You can delete it and nothing will change.
In menu, you say cin >> choice;, where choice is of type int. The stream operator >> works like this: It will try to read as many characters as it can, such that the characters match the requested type. So this will only read ints.
Then you convert your valid int into a string and call isNumber() - which will alway return true.
So if you wish to read any line of text and handle it, you can use getline():
string inp;
std::getline(std::cin, inp);
if (!isNumber(inp)) {
std::cout << "ERROR\n";
return 1;
}
int choice = std::stoi(inp); // May throw an exception if invalid range
See stoi
Your isNumber() implementation could look like this:
#include <algorithm>
bool is_number(const string &inp) {
return std::all_of(inp.cbegin(), inp.cend(),
[](unsigned char c){ return std::isdigit(c); });
}
If you are into that functional style, like I am ;)
EDIT:
Btw., another bug which the compiler warns about: cout << "Your balance is $" << balance; "\n\n"; - the newlines are separated by ;, so it's a new statement and this does nothing. You probably wanted the << operator instead.
Recursive call bug:
In { cout << "Invalid choice of operation."; menu(); } and same for ReturnCard(), the function calls itself (recursion).
This is not at all what you want! This will start the function over, but once that call has ended, you continue where that call happened.
What you want in menu() is to start the loop over. You can do that with the continue keyword.
You want the same for ReturnCard(). But you need a loop there.
And now, that I read that code, you don't even need to convert the input to an integer. All you do is compare it. So you can simply do:
string inp;
std::getline(std::cin, inp);
if (inp == "1" || inp == "2") {
// good
} else {
// Invalid
}
Unless that is part of your task.
It is always good to save console input in a string variable instead of another
type, e.g. int or double. This avoids trouble with input errors, e.g. if
characters instead of numbers are given by the program user. Afterwards the
string variable could by analyzed for further actions.
Therefore I changed the type of choice from int to string and adopted the
downstream code to it.
Please try the following program and consider my adaptations which are
written as comments starting with tag //CKE:. Thanks.
#include <iostream>
#include <string>
using namespace std;
bool isNumber(const string& s) //function to determine if input value is int
{
for (size_t i = 0; i < s.length(); i++) //CKE: keep same variable type, e.g. unsigned
if (isdigit(s[i]) == false)
return false;
return true;
}
bool ReturnCard() //function to determine whether to continue running or end program
{
string rtrn;
cout << "\nDo you wish to continue? \n1 - Yes \n2 - No, return card" << endl;
cin >> rtrn;
if (rtrn == "1" and isNumber(rtrn)) { return false; }
if (rtrn == "2" and isNumber(rtrn)) { return true; } //CKE: remove redundant else
cout << "Invalid choice." << endl; ReturnCard(); //CKE: remove redundant else + semicolon
return false;
}
int menu() //function for operation choice and execution
{
string choice; //CKE: change variable type here from int to string
do
{
cout << "\nPlease select an operation:\n" << endl
<< " 1 - Balance inquiry\n"
<< " 7 - Return card\n"
<< "\nEnter your choice and press return: ";
int balance = 512;
cin >> choice;
if (choice == "1" and isNumber(choice)) { cout << "Your balance is $" << balance << "\n\n"; } //CKE: semicolon replaced by output stream operator
else if (choice == "7" and isNumber(choice)) { cout << "Please wait...\nHave a good day." << endl; return 0; }
else { cout << "Invalid choice of operation."; } //CKE: remove recursion here as it isn't required
} while (!ReturnCard()); //CKE: negate result of ReturnCard function
cout << "Please wait...\nHave a good day." << endl;
return 0;
}
int main()
{
string choice;
cout << "Insert debit card to get started." << endl;
menu();
return 0;
}
I am trying to get input from the user and need to know a way to have the program recognize that the input was or was not a double/char this is what i have right now... but when you type an incorrect type of input
1) the double test one just loops infinatly
2) the char one won't stop looping even with the correct imput
int main () {
double _double = 0;
bool done = true;
while ( done ) {
cout << "Please enter a DOUBLE:\n" << endl;
cin >> _double;
if ( _double > 0 ) { done = false; }
if ( _double < 0 ) { cout << "\nthe number you entered was less than zero\nplease enter a valad number..." << endl; }
if(cin.fail()) { cin.clear(); }
}
done = false;
char _char = ' ';
while ( !done ) {
cout << "Please enter a CHAR" << "\n";
cout << "\t'y' = yes\n\t'n' = no" << endl;
cin >> _char;
if ( _char == 'y' || _char == 'n' ) { done = true; }
if ( ! (_char == 'y' || _char == 'n') ) { cout << "\nyou have entered an invald symbol... \n" << endl; }
if(cin.fail()) { cin.clear(); }
}
The best bet is always to read your input as strings. You can then use functions like std::strtod() to test and convert to doubles. Checking if streams have failed and then resetting them is error prone at best, and doesn't give you the possibility of producing good error messages.
For example:
string s;
cin >> s;
char * p;
double d = strtod( s.c_str(), & p );
if ( * p == 0 ) {
cout << "Read double: " << d << endl;
}
else {
cout << "Read string: " << s << endl;
}
The pointer 'p' will point to the first character that cannot be converted to a double. How exactly you handle that really depends on your app's logic.
The problem is that when you read something and cin sees the input can never be a double, it stops reading, leaving the stuff in the buffer that it didn't consume. It will signal failure, which you clear but you won't eat the remaining input that cin didn't eat up. So, the next time the same wrong input is tried to read again, and again...
The problem with the char one is that you have to press the return key to make it process any characters on most terminals (this does not happen if you make your program read from a file, for instance). So if you press y then it won't go out of the read call, until you hit the return key. However, then it will normally proceed and exit the loop.
As others mentioned you are better off with reading a whole line, and then decide what to do. You can also check the number with C++ streams instead of C functions:
bool checkForDouble(std::string const& s) {
std::istringstream ss(s);
double d;
return (ss >> d) && (ss >> std::ws).eof();
}
This reads any initial double number and then any remaining whitespace. If it then hit eof (end of the file/stream), it means the string contained only a double.
std::string line;
while(!getline(std::cin, line) || !checkForDouble(line))
std::cout << "Please enter a double instead" << std::endl;
For the char, you can just test for length 1
std::string line;
while(!getline(std::cin, line) || line.size() != 1)
std::cout << "Please enter a double instead" << std::endl;
If you want to read only 1 char and continue as soon as that char was typed, then you will have to use platform dependent functions (C++ won't provide them as standard functions). Look out for the conio.h file for windows for instance, which has the _getch function for this. On unix systems, ncurses provides such functionality.
cin >> _double will always get you a double, whether they typed in "42", "0" or "mary had a little lamb". You need to read the user input as a string, then test that string to see if it is a double. sscanf will return 0 if it can't convert the input string to the desired type:
cout << "Please enter a DOUBLE:\n" << endl;
string s;
cin >> s;
if( !sscanf(s.c_str(), "%lf", &_double) )
{
done = false;
cout << "Not a number, sparky. Try again." << endl;
continue;
}
Also, identifiers with leading underscores like you have are reserved by the language. Don't get in the habit of naming things like _double -- someday, they may not work.
I'm am writing a program for a homework assignment, and can't quite understand what I am doing wrong. The program takes some text input, and outputs it in pig latin, and while the program now achieves this, there is stack corruption on program termination, with VS mentioning "Run-Time Check Failure #2 - Stack around the variable 'token' was corrupted."
I suspect that this is due to going past the boundaries of the pointer somehow, but I don't entirely understand pointers at the current moment, so most things I found don't really make sense, and I'm trying to understand, not just fix what I did wrong.
Attached is the function using the variable that is causing all the trouble (edited to include full program, due to me realizing that some important bits were left out).
int main(void)
{
char text[] = "";
char seps[] = " \t\n";
char *token = NULL;
char *next_token = NULL;
bool cont = true;
bool valid = false;
char contResp;
cout << "Enter a sentence to be translated: ";
do
{
cin.getline(text, 200);
cout << endl << text << endl;
token = strtok_s(text, seps, &next_token);
while (token != NULL)
{
if (token != NULL)
{
printLatinWord(token);
token = strtok_s(NULL, seps, &next_token);
}
}
cout << endl << endl << "Do you want to enter another sentence (y/n)? ";
valid = false;
while (!valid)
{
cin >> contResp;
contResp = tolower(contResp);
if (contResp == 'y')
{
valid = true;
cin.ignore();
cout << "Enter a sentence to be translated: ";
}
else if (contResp == 'n')
{
valid = true;
cont = false;
}
else
{
cout << "Invalid response. Please try again.";
cout << endl << endl << "Do you want to enter another sentence (y/n)? ";
}
}
}
while (cont);
system("pause");
return 0;
}
void printLatinWord(char *token)
{
string text = "";
char *first = token;
token++;
while (*token != '\0')
{
text += *token;
token++;
}
text += *first;
text += "ay ";
cout << text;
}
I am not sure how to get this problem sorted out, but if I could get some help as well as a simple explanation of what I did wrong, I would greatly appreciate it, as pointer arithmetic is mostly gibberish to me.
Thanks in advance!
char text[] = "";
This creates a 1-byte array to hold a '\0' character (NUL terminator). It’s the same as:
char text[1];
text[0] = '\0';
cin.getline(text, 200);
This writes up to 200 chars — 199 chars plus the NUL terminator — into the 1-char array.
Obvious solution: make the array 200 chars long.
char text[200] = "";
Alternatively, use a std::string for text instead of a char array, and use getline(cin, text); for unlimited line length.
I'm probably an idiot, I know. This is probably a duplicate, but for the life of me I couldn't find an answer after looking for over an hour. I guess I don't know how to phrase a search. All I want is to be able to print out a string from a function based on user input. This isn't part of the program I'm building right now, but it illustrates my problem.
#include "../std_lib_facilities.h"
string make_name()
{
string name;
int selection;
cout << "Enter 1 steve. Enter 2 Mike.\n\n";
cin >> selection;
if (selection == '1')
name = "Steve";
else if (selection == '2')
name = "Mike";
return name;
}
int main()
{
cout << "Make a name\n";
make_name();
cout << "Your name is " << make_name;
keep_window_open();
}
Instead of printing Mike or Steve, it prints nonsense. I've been working on all the other parts of my program, but now this is the only problem I have. So, lay into me. What am I doing wrong?
The reason you're getting garbage printed is because at this line in main:
cout << "Your name is " << make_name;
You're printing out the address of the function 'make_name', as opposed to the result of calling it.
The fix is to capture the result of calling the function and print that out instead:
int main()
{
cout << "Make a name\n";
string name = make_name();
cout << "Your name is " << name;
keep_window_open();
}
One other latent issue is inside of your 'make_name' function, your two conditions:
if (selection == '1')
name = "Steve";
else if (selection == '2')
name = "Mike";
You're comparing 'selection', which is an int, to the ASCII characters 1 and 2, which is not what you want.
Change that to:
if (selection == 1)
name = "Steve";
else if (selection == 2)
name = "Mike";
and you should have the desired result.
Note: Also, consider initialising your 'selection' variable to 0. That should prevent unexpected results if someone were to input "hello", instead of a number.
This is because of
if (selection == '1')
Here you are comparing an int with char. That means value of '1' would be the ASCII value of this character which is not 1.
Correct conditional statement should be:-
if (selection == 1)
Sorry if it's something simple, but I'm new to C++ and haven't really gotten a good hold on it, yet. I need to build a calculator whose only named variables are pointers, and this is what I have so far, but I keep getting errors and I can't figure out why. Every error that always related to my if construct, though.
int main()
{
//Creating variables
//Values to perform operations on
float *aptr = new(nothrow)float;
float *bptr = new(nothrow)float;
float *ansptr = new(nothrow)float;
int *endptr = new(nothrow)int;
char *operationptr = new(nothrow)char;
cout << "Simple Operation Calculator" << endl; //Displays program name
cout << "Performs +, -, *, or / on any two real operands." << endl; //Describes nature of program to user
*endptr = 1;
while(*endptr = 1) //Creates a loop so that the user may perform as many operations as desired
{
//Prompts the user for the first operand
cout << "First operand: " << endl;
cin >> *aptr;
//Prompts user for operator
cout << "Operator(+,-,*,/): " << endl;
cin >> *operationptr;
//Prompts user for second operand
cout << "Second operand: " << endl;
cin >> *bptr;
//Performs requested operation
if(*operationptr == '+' || *operationptr == 'plus')
{
*ansptr = *aptr + *bptr;
}
else if(*operationptr == '-' || *operationptr == 'minus')
{
*ansptr = *aptr - *bptr;
}
else if(*operationptr == '*' || *operationptr == 'times')
{
*ansptr = *aptr * *bptr;
}
else if(*operationptr == '/' || *operationptr == 'divided by')
{
if(*bptr = 0)
{
cout << "Cannot divide by zero. Terminating program." << endl;
*endptr = 2;
break;
}
*ansptr = *aptr / *bptr;
}
else
{
cout << "Invalid operand input. Terminating program." << endl;
*endptr = 2;
break;
}
//Displays results
cout << *aptr << *operationptr << *bptr << " = " << *ansptr << endl;
//Asks user if they wish to perform another operation. If so, they stay in loop. If not, then break from loop.
cout << "Do you wish to perform another operation? (1 = yes, 2 = no)" << endl;
cin >> *endptr;
//If 1, loop repeats. If 2, program ends.
if (*endptr == 2)
{
cout << "Thank you for using my program. Goodbye!" << endl;
}
} //end while loop
return 0;
}//end main function
There are character literals (with ') and string literals (with "). Character literals have one character. String literals are arrays of characters. You can't write something like 'plus' because it has more than one character (well technically you can, but it's a multi-character literal, but lets not go there).
Nonetheless, this wouldn't make any sense because operationptr points at a single char object. A single char can't contain the entire word plus.
If you want to be able to accept plus as input, then I suggest you start using strings. In fact, use std::string.
As a side note, you are using pointers and dynamic allocation far too often. You are also forgetting to delete the objects that you create with new - this is a memory leak. I imagine you have come from a language that uses new for all object creation. In C++, this is not necessary (and is not a good practice). Instead, you can declare objects like so:
float aptr;
There is no need to dereference this object. You can just use aptr directly as a float.
'plus'
is a character constant, and can't contain more than one character.
'+' is fine, since it's a single character in a constant.
As per the comment on this answer,
'plus' could be ok, if the compiler is not expecting a char.