C++ while loop insight request - c++

basically I am having weird trouble with my while loop near the beginning of the program which checks for user validation on their choice of activity. When they choose the first activity and complete it, it works fine, but when they complete the second activity, it will go into runtime and keep requesting the user to input a valid choice, even though they haven't even gotten the chance to input a choice. Any tips?
#include <iostream>
using namespace std;
int main()
{
const int DIGITS_CHOICE = 1, IDENTIFIER_CHOICE = 2, DOUBLE_CHOICE = 3, EXIT_CHOICE = 4;
int choice;
int userNumber, storedNumber, factor = 10, digitCounter = 0, subtractor;
char ch;
do
{
cout << "\n\n\t\tPlease choose an option:\n\n"
<< "1. How many digits?\n"
<< "2. Is this a valid C++ Identifer?\n"
<< "3. Is this a double letter word?\n"
<< "4. Exit\n";
cout << endl << "Choice: ";
cin >> choice;
while (choice < DIGITS_CHOICE || choice > EXIT_CHOICE)
{
cout << endl << "Please enter a valid menu option: ";
cin >> choice;
}
if (choice != EXIT_CHOICE)
{
switch (choice)
{
case DIGITS_CHOICE:
cout << "Please enter an integer: ";
cin >> userNumber;
storedNumber = userNumber;
if (userNumber < 10)
{
digitCounter = 1;
}
else
{
while (userNumber != 0)
{
subtractor = userNumber % factor;
if (subtractor > 0)
{
userNumber = userNumber - subtractor;
factor *= 10;
digitCounter++;
}
else
{
userNumber = 0;
}
}
}
cout << storedNumber << " has " << digitCounter << " digit(s)." << endl;
factor = 10;
digitCounter = 0;
break;
case IDENTIFIER_CHOICE:
cout << "Please enter an identifier and press [Enter] immediately after. ";
cin >> ch;
if (ch >= 0 || ch <= 9 || ch <= 'a' || ch >= 'z' || ch <= 'A' || ch >= 'Z' || ch != '_')
{
if (ch >= 0 || ch <= 9)
{
cout << "Not a valid identifier." << endl;
cout << "Identifiers cannot start with a digit." << endl;
ch = '\n';
}
else
{
cout << "Not a valid identifier." << endl;
cout << "Inavlid character." << endl;
ch = '\n';
}
}
while (ch != '\n')
{
if (ch >= 'a' || ch <= 'z' || ch >= 'A' || ch <= 'Z')
{
cin.get(ch);
}
}
break;
case DOUBLE_CHOICE:
break;
}
}
} while (choice != EXIT_CHOICE);
return 0;
}
Also the program isn't complete yet. the third option has nothing and the 2nd option is almost complete. the first activity though is complete :)

Your check for valid characters is too broad, and doesn't really make sense:
if (ch >= 0 || ch <= 9 || ch <= 'a' || ch >= 'z' || ch <= 'A' || ch >= 'Z' || ch != '_')
Every possible value of ch is going to be greater than or equal to zero, so this expression is equivalent to (true || a || b || c || ... || z) and it's always going to resolve to true.
Instead, see if it's below 'A', between 'Z' and 'a' or beyond 'z' and if so, it's invalid.
Also, when checking if it's a digit, you need to check if it's ≥ '0' and ≤ '9' as characters. It's important that you compare it to the character representation of 0 and 9 because the value of the character '0' not actually 0 (it turns out it's actually 48) and likewise with '9':
if ( ch < 'A'
|| (ch > 'Z' && ch < 'a')
|| ch > 'z')
{
if (ch >= '0' && ch <= '9')
{
cout << "Not a valid identifier." << endl;
cout << "Identifiers cannot start with a digit." << endl;
ch = '\n';
}
else
{
cout << "Not a valid identifier." << endl;
cout << "Invalid character." << endl;
ch = '\n';
}
}
It's not really clear what the check after that is meant to do? Is it only meant to allow letters? That seems strange after saying "Identifiers cannot start with a digit." anyway:
if (ch >= 'a' || ch <= 'z' || ch >= 'A' || ch <= 'Z')
This has essentially the same issue where every character is going to be either above 'a' or below 'z' or both, so this will always resolve to true. Instead, use && to check for being within a range:
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
Hopefully that addresses your question.

The logic is not great; i is valid, but your code says it's not. It at least goes back to the menu just fine. Your code is also behaving like it will respond immediately as letters are being typed. That is not the case. It won't print anything until the user presses Enter.
It seems the issue is your variable ch being a char. If I type anything that's longer than a single character, what will happen is that the single character gets evaluated, and the remaining characters I typed remain in the input stream. It looks like you are attempting to handle that, but it's not working. I am not going to spend time delving into the why, partly because it's complex, partly because I don't know the full intricacies of istream behavior.
What I will say is that if you want to handle a multi-character input, use cin.get() everywhere and not just sometimes. You can do processing of each character, but again, nothing will go to the screen until the user presses Enter.
But here's code that appears to work:
#include <cctype> // isalpha() and isalnum()
#include <string> // ch is now a std::string
// ...
case IDENTIFIER_CHOICE:
cout << "Please enter an identifier and press [Enter] immediately after. ";
std::cin.ignore(256, '\n'); // needed because of getline behavior
std::getline(std::cin, ch);
if (!(isalpha(ch[0]) || ch[0] == '_')) {
cout << "Not valid.\n";
break;
}
for (int i = 1; i < ch.size(); ++i) {
if (!isalnum(ch[i])) {
cout << "Not valid.\n";
break;
}
}
cout << "Valid.\n";
break;
// ...
With cin, when you press Enter, that keystroke is saved in the input stream. getline() doesn't behave the way we expect because while cin will typically ignore that keystroke, getline does not. So I just tell cin to ignore an arbitrary (but sufficient in this case) amount of characters in the stream up to and including the Enter keystroke (Mac and Linux, should still behave for Windows (I think)).
This is still far from bulletproof input validation (that's impossible), but I think it suffices for what you're working on.

Related

How to prevent C++ buffer overflow if the user enters two or more strings separated by white spaces?

I'm a student, and I am currently working on C++ Classes. I am making a program which is supposed to ask a user to input a float point number not greater that 99.99 as a price of fuel at a gas station. I have created code that saves the user input in to a char array, and created limits so that the user can't input more than 2 dots, for example (2..2). The maximum number of characters is 5 including one dot. Now, everything works fine except for if the user enters two sets of strings before hitting enter. I have a problem because the second string messes up with other cin statements in the loop.
The code will also take the finalized char array input, and than covert it to a float variable so that the further calculations can be computed easily.
I am working on a Windows system, and Visual Studio 2017 C++.
I have tried detecting the single white space in an if/else statement, but it seems that white space is not detected as a single char array member, like this ex. else if (str[count] == ' ') , and than asking to re enter the correct input without the white space. getline() function could not work on a char array, so I couldn't discard the characters entered after including and after the white space in this way. I have tried changing the char array to a string, but still if the user inputs two or more strings separated by white space, my program keeps reading it in to cin over again.
int main()
{
int count = 0;
int lenFlag = 0, mainFlag = 0;
float result = 0;
int len;
char str[6] = "00000";
//string str ="00000";
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//This part will ask the user to set the price of fuel
//for the gas pump program. The programming project segment
//is from a book by Walter Savitch "Absolute C++".
while (mainFlag == 0)
{
cout << "Please enter the fuel price in dollars $";
cin >> str;
len = strlen(str);
cout << "strlen is = " << len << endl;
while (len <= 5 && mainFlag == 0)
{
count = 0, lenFlag = 0;
while (count < len && lenFlag == 0)
{
if (count == 0 && (str[count] < 48 || str[count] > 57))
{
cout << "The first input member must be a number."
"You must use a number between 0-9.\n"
"Try again: ";
cin >> str;
len = strlen(str);
lenFlag = 1;
}
else if (count > 0 && (str[count] < 48 || str[count] > 57)
&& str[count] != '.')
{
cout << "You must enter number between 0-9, or a decimal delimiter.\n"
"Try again, : ";
cin >> str;
len = strlen(str);
lenFlag = 1;
}
else if (count > 0 && (str[0] == '.' && str[1] == '.') || (str[0] == '.' && str[2] == '.') ||
(str[0] == '.' && str[3] == '.') || (str[0] == '.' && str[4] == '.') ||
(str[1] == '.' && str[2] == '.') || (str[1] == '.' && str[3] == '.') ||
(str[1] == '.' && str[4] == '.') || (str[2] == '.' && str[3] == '.') ||
(str[2] == '.' && str[4] == '.') || (str[3] == '.' && str[4] == '.'))
{
cout << "You have entered more than 1 decimal delimiter, try again: ";
cin >> str;
len = strlen(str);
lenFlag = 1;
}
else if (count > 1 && str[0] > 48 && str[0] < 58 && str[1]>47
&& str[1] < 58 && str[2]>47 && str[2] < 58)
{
cout << "Maximum number is 99.99, try again:\n";
cin >> str;
len = strlen(str);
lenFlag = 1;
}
else if (str[count] == ' ')
{
cout << "Typing whitspace is not an option!!" << endl;
cout << "Try again!!" << endl;
cin >> str;
len = strlen(str);
lenFlag = 1;
}
else if (count == len - 1 && lenFlag == 0)
{
//cout << "Main flag switches to 1!!" << endl;
mainFlag = 1;
}
count++;
}
} //while(lenCopy <= 5) loop end
if (len > 5)
{
cout << "Either non-numbers were entered, or a negative
number, or an incorrect " << endl;
cout << "number size. Enter a maximum size of 5
including a .dot for decimal number" << endl;
cout << "Maximum number is 99.99." << endl;
mainFlag = 0;
}
}//mainflag loop ends
int dotpos = 0;
for (int n = 0; n < len; n++)
{
if (str[n] == '.')
{
//dotpos = n + 1;
dotpos = len - n - 1;
cout << "dotpos = " << dotpos << endl;
}
else
{
result = result * 10 + (str[n] - '0');
//Line above is a float and character mix as a math equation.
cout << "result " << n << " = " << result << endl;
}
}
if (dotpos > 0)
result = result / (power(10, dotpos));
cout << "You have set the cost at $" << result << " per gallon." << endl;
system("pause");
return 0;
}
Occasional stack around str variable has been corrupted, and that happens when I heavily try to mess with the user input just to check if the program can crash. That's why I need to know how to clear the input after the white space. I solved the stack corruption problem by changing the char array to string, but still not the excess characters that potential users might throw down at the program.
If you must use character arrays, I highly recommend restricting the amount of characters read from the console.
The std::istream::getline() is well suited for this:
const unsigned int LIMIT = 10;
char number_as_text[LIMIT];
std::cout << "Enter a floating point number, less than 10 characters: ";
std::cin.getline(number_as_text, LIMIT);
You can then use a function like strtod to convert the string to floating point variable.
I have found one good way to solve a problem of the string buffer overflow. It uses
cin>>ws; followed by getline() function. The two need to be used in conjunction, and
than the read will be only the first string of characters and everything after the whitespace will be trashed.
cout << "Do you want to set cost in gallons or liters? "
"\nPress G for gallons or L for liters: ";
cin >> ws;
getline(cin, chooseSetCost);
while (chooseSetCost != "G" && chooseSetCost != "g" && chooseSetCost != "L" && chooseSetCost != "l")
{
cout << "Incorrect input. Try again: ";
cin >> ws;
getline(cin, chooseSetCost);
cout << "choose cost is = " << chooseSetCost << endl;
}

Fixing uninitialized local variable error

I am working on a project right now and when I try to run what I have below it gives me an error that says "uninitialized local variable 'userOption' used" on line 22, while (isValidOption(userOption) == true) {.
How do I fix that error? Thank you.
#include<iostream>
#include <string>
using namespace std;
char toupper(char ch) {
if (ch >= 'A'&&ch <= 'Z')
return(ch);
else
return(ch - 32);
}
bool isValidOption(char ch) {
if (ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X')
return(true);
else
return(false);
}
char getMainOption() {
string UserInput;
char userOption;
while (isValidOption(userOption) == true) {
cout << "Choose One of the following options\n";
cout << "I--List Our Inventory\n";
cout << "O--Make an Order\n";
cout << "L--List all Orders made\n";
cout << "X--Exit\n";
cout << "Enter an option: ";
getline(cin, UserInput);
userOption = toupper(UserInput[0]);
if (!isValidOption(userOption)) {
cout << "Invalid String\n";
cout << "Enter an option: ";
getline(cin, UserInput);
userOption = toupper(UserInput[0]);
}
if (userOption == 'I')
cout << "Listing Our Inventory\n";
else if (userOption == 'O')
cout << "Make an order\n";
else if (userOption == 'L')
cout << "Listing all orders\n";
}
return userOption;
}
int main() {
char choice;
choice = getMainOption();
system("pause");
return 0;
}
What the error is saying that you're trying to read from userOption before you've ever written to it. If a variable is uninitialized, its memory contents will be full of junk left behind by other functions and it can easily cause bugs. In your case, you'll want to read input from the user into userOption before you do any logic on it. This can be done with a do-while loop:
char userOption; // not yet initialized
do {
...
cin >> userOption; // userOption gets initialized here on first loop run
} while (isValidOption(userOption)); // no need for == true, that's a tautology :-)
// NOTE: perhaps you want to loop while the input is INvalid, as in
// while (!isValidOption(userOption)); ?
A couply code-review comments I would additionally give are:
std::toupper already exists in <cctype>. Docs are here
return is not a function call and it's better to write return ch; than return(ch);
if (ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X'){ return true; } else { return false; } is completely equivalent to the shorter return ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X';
Also take a look at system(“pause”); - Why is it wrong?
Happy coding! Let me know if questions remain

What is the purpose of "input.length();"?

I am taking a college level C++ course, and quite frankly nothing is really ever explained. I was given code to write, and my program works as it should. I would just like to know the purpose of certain lines.
Such as:
int i = 0;
I know I am declaring an int variable that = 0. Here my question is why the letter i? Could that be any variable name I choose?
int length = input.length();
I know I am declaring an int variable named length... but what purpose does it serve in my code?
i++
I think this ends my loop?
I have added my code for perusal. Any assistance would be greatly appreciated!
// Program takes user entered letter and matches it with the corresponding ICAO word.
//Program has been modified to use void and string methods
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
//Function Heading
void convert(string);
//Main Function
int main()
{
string input;
cout << " Enter a letter or word: "; // Ask the user to enter a letter or word.
cin >> input; //get input
cout << "Phonetic Version : "; //Display "Phonetic Version"
convert (input);
cout << endl;
system("pause");
}//End Main
//Function Definition
void convert(string input)
{
int i = 0; //input variable
char letters; //character variable
int length = input.length();
while (i < length) //While loop initialized
{
letters = input.at(i);
if (letters == 'a' || letters == 'A')
cout << "Alpha ";
else if (letters == 'b' || letters == 'B')
cout << "Bravo ";
else if (letters == 'c' || letters == 'C')
cout << "Charlie ";
else if (letters == 'd' || letters == 'D')
cout << "Delta ";
else if (letters == 'e' || letters == 'E')
cout << "Echo ";
else if (letters == 'f' || letters == 'F')
cout << "Foxtrot ";
else if (letters == 'g' || letters == 'G')
cout << "Golf ";
else if (letters == 'h' || letters == 'H')
cout << "Hotel ";
else if (letters == 'i' || letters == 'I')
cout << "India ";
else if (letters == 'j' || letters == 'J')
cout << "Juliet ";
else if (letters == 'k' || letters == 'K')
cout << "Kilo ";
else if (letters == 'l' || letters == 'L')
cout << "Lima ";
else if (letters == 'm' || letters == 'M')
cout << "Mike ";
else if (letters == 'n' || letters == 'N')
cout << "November ";
else if (letters == 'o' || letters == 'O')
cout << "Oscar ";
else if (letters == 'p' || letters == 'P')
cout << "Papa ";
else if (letters == 'q' || letters == 'Q')
cout << "Quebec ";
else if (letters == 'r' || letters == 'R')
cout << "Romeo ";
else if (letters == 's' || letters == 'S')
cout << "Sierra ";
else if (letters == 't' || letters == 'T')
cout << "Tango ";
else if (letters == 'u' || letters == 'U')
cout << "Uniform ";
else if (letters == 'v' || letters == 'V')
cout << "Victor ";
else if (letters == 'w' || letters == 'W')
cout << "Whiskey ";
else if (letters == 'x' || letters == 'X')
cout << "X-ray ";
else if (letters == 'y' || letters == 'Y')
cout << "Yankee ";
else if (letters == 'z' || letters == 'Z')
cout << "Zulu ";
i++;
}
}
int length = input.length();
I know I am declaring an int variable named length... but what purpose
does it serve in my code?
None.
It would serve some purpose if input's length would change later on and you'd need to remember the old length for some reason.
Since this is not the case here, your professor may think that this is some kind of "optimisation" on the grounds that repeatedly calling length() may be too slow. But this is nonsense; your computer is too fast for such micro-optimisations to have an observable effect, especially with modern compilers being much better at optimising programs than the programmers themselves.
Just remove the length variable to make the code shorter.
Here
string input;
std::string has a method called length() which returns the length of the string, in terms of bytes. Hence you are using like
int length = input.length(); /* use variable name as other than predefined method to avoid confusion */
| |
this is just this is a method of string
a int variable
int i = 0;
I know I am declaring an int variable that = 0. Here my question is why the letter i? Could that be any variable name I choose?
Yes. Variable names are arbitrary, name them however you want (within the restrictions of the language syntax, of course). Just make sure you use names that are meaningful within the context in which they are being used. Readability matters when maintaining code over time.
int length = input.length();
I know I am declaring an int variable named length... but what purpose does it serve in my code?
To make a local cached copy of the character count of the input string so your loop does not have to keep calling the string's length() method over and over. Using a few bytes of local stack space can save time and overhead of retrieving the string's length, which does not change while the loop runs.
i++
I think this ends my loop?
It increments the value of the i variable, nothing more. The loop ends when the while statement evaluates as false (when i catches up to length).

weird braces/bracket symbol at the end of my output. (my program is on printing out the next alphabet).

#include <iostream>
using namespace std;
int main()
{
char input, letter1,letter2;
cout << "Enter a letter: ";
cin >> input;
if ( (input >= 'A' && input >= 'Z') || (input <= 'a' && input <= 'z') )
{
if ( (input >= 'A' && input >= 'X') || (input <= 'a' && input <= 'x') )
{
letter1 = input + 1;
letter2 = input + 2;
cout << "Your letter trio today is " << input << letter1 << letter2<<".\n";
}
else if ( input == 'Y' || input == 'y' )
{
letter1 = input + 1;
cout << "Your letter trio today is " << input << letter1 <<".\n";
}
else
{
cout << "Your letter trio today is " << input <<".\n";
}
}
}
if I input y, my output becomes yz{. If I input Y, my output becomes YZ[. If I input Z, my output becomes Z[. Any ideas? thanks guys
Welcome to the ASCII representation of characters! C++ standard only requires that representation of digits are contiguous, but most common implementation use ASCII. The ASCII code for Y is 0x59. 0x59+1 is 0x5A the ASCII code of Z. And just guess what is the character with ASCII code 0x5B? Yes it is [ so your output is normal...
The same, ascii code of lower case y is 0x79, and ascii code of { is 0x7B

Need to read white space and other chars C++

I have been working on this all day with no luck. Its night now and don't know what to do. My assignment is to read number of vowels, number of white spaces, and number of other characters in a user inputted sentence. I know i need to use cin.get(ch) for whitespaces, but don't know how. I also need to output the sentence to a file. Heres what I have so far:
//Get data from user
cout << "Enter your sentence on one line followed by a # to end it: " << endl;
while (cin >> noskipws >> character && character != '#')
{
character = static_cast<char>(toupper(character));
if (character == 'A' || character == 'E' || character == 'I' ||
character == 'O' || character == 'U')
{
vowelCount++;
isVowel = true;
}
if (isspace(character))
{
whiteSpace++;
}
else if (isVowel == true && isspace(character))
{
otherChars++;
}
outFile << character;
}
outFile << "vowelCount: " << vowelCount << endl;
outFile << "whiteSpace: " << whiteSpace << endl;
outFile << "otherchars: " << otherChars << endl;
This line
if (character == 'A' || 'E' || 'I' || 'O' || 'U');
Is not doing what you think. It will always return true.
you need
if (character == 'A' || character == 'E' || character == 'I' || character == 'O' || character =='U')
and remove the semicolon as well at the end of that line
Here:
while (cin >> character && character != '#')
You are skipping all white space. To prevent the operator >> from skiiping white space you need to explicitly specify this with the noskipws modifier.
while(std::cin >> std::noskipws >> character && character != '#')
Alternatively the same affect can be achieved with get
while(std::cin.get(character) && character != '#')
Next you are reading more characters outside the loop condition.
cin.get(character);
You already have a value in the variable 'character'. So remove both of these. The next iteration of the loop (in the while condition) will get the next character (as it is executed before the loop is entered).
Then fix you test as Tim pointed out.
You can then add another test for white space with:
if (std::isspace(character)) // Note #include <cctype>
{ /* STUFF */ }
#include <iostream>
using namespace std;
int main()
{
char ch;
int vowel_count = 0;
int space_count = 0;
int other_count = 0;
cout << "Enter a string ends with #: " << endl;
while(1)
{
cin.get(ch);
if(ch == '#')
{
break;
}
if(ch == 'A' || ch == 'a'
|| ch == 'E' || ch == 'e'
|| ch == 'I' || ch == 'i'
|| ch == 'O' || ch == 'o'
|| ch == 'U' || ch == 'u')
{
++vowel_count;
}
else if(ch == ' ')
{
++space_count;
}
else
{
++other_count;
}
}
cout << "Vowels: " << vowel_count << endl;
cout << "White spaces: " << space_count << endl;
cout << "Other: " << other_count << endl;
return 0;
}
No arrays
You can check for whitespace the exact same way. Common whitespace characters are space (' '), and horizontal tab ('\t'). Less-common are newline ('\n'), carriage return ('\r'), form feed ('\f') and vertical tab ('\v').
You can also use isspace from ctype.h.