So I have this part of code and I have these error than I'm to post how can I fix them . Thx for ur help..
void DisplayMenu()
{
cout <<"Please choose from the following options :\n\n"
<<"1-al.\n"
<<"2-c.\n"
<<"3-v.\n\n"
<<"Or I want to see first :\n"
<<"------------------------\n\n"
<<"4-r.\n"
<<"5-m.\n"
<<"6-k.\n"
<<"7-d.\n"
<<"8-u.\n\n"
<<"Or :\n"
<<"----\n\n"
<<"9-I changed my mind and would like to exit.\n\n";
}
int ChooseFromMenu()
{
int A =0 ;
while ((DisplayMenu() && !(cin >> A ) || (cin.good() && (A < 1 || A > 9))) {
cout << "\nInvalid input!\n"<<endl;
cin.clear();
cin.ignore(1000, '\n');
}
return A;
}
and this is my error list
3 IntelliSense: expected a statement
2 IntelliSense: expected a ')'
Error 1 error C4716: 'DisplayMenu' : must return a value
If you really want to use DisplayMenu in the condition of the while loop, you need to use the comma operator:
int ChooseFromMenu()
{
int A =0 ;
while (DisplayMenu(), (!(cin >> A ) || (cin.good() && (A < 1 || A > 9))) {
cout << "\nInvalid input!\n"<<endl;
cin.clear();
cin.ignore(1000, '\n');
}
return A;
}
This will call DisplayMenu, throw the (non-existant) return value away, and then evaluate the actual condition. Personally however, I would use an infinite loop with a conditional break inside:
int ChooseFromMenu()
{
int A =0;
while (true) {
DisplayMenu();
if ((cin >> A)) {
if (!cin.good() || (1 <= A && A <= 9)) {
return A;
}
}
cout << "\nInvalid input!\n"<<endl;
cin.clear();
cin.ignore(1000, '\n');
}
}
I have split the condition into two embedded ifs because I find that easier to read than combinations of && and ||. (I also like to always use < or <= when combining multiple comparisons - I find it makes it easier to read.)
You're missusing condition in your while loop parameter :
while ((DisplayMenu() && !(cin >> A ) || (cin.good() && (A < 1 || A > 9)))
As you can see from the definition your DisplayMenu is returning void and you are trying to compare it to boolean value ( here -> while ((DisplayMenu() ).
To solve this you can change your DisplayMenu method to just return boolean value :
bool DisplayMenu()
{
// your logic
return true;
}
Complete working example :
#include <iostream>
using namespace std;
int DisplayMenu()
{
cout <<"Please choose from the following options :\n\n"
<<"1-al.\n"
<<"2-c.\n"
<<"3-v.\n\n"
<<"Or I want to see first :\n"
<<"------------------------\n\n"
<<"4-r.\n"
<<"5-m.\n"
<<"6-k.\n"
<<"7-d.\n"
<<"8-u.\n\n"
<<"Or :\n"
<<"----\n\n"
<<"9-I changed my mind and would like to exit.\n\n";
return 1;
}
int ChooseFromMenu()
{
int A =0 ;
if ((DisplayMenu() && !(cin >> A )) || (cin.good() && (A < 1 || A > 9))) {
cout << "\nInvalid input!\n"<<endl;
cin.clear();
cin.ignore(1000, '\n');
}
return A;
}
You forgot a parenthesis in the while loop in ChooseFromMenu(). That is probably causing 2 of the errors. Since return true didn't work, you probably forgot to change the method header from void to boolean. (cin >> A) also returns void.
Related
I know for a fact similar questions have been asked before but I really can't figure out what's wrong with my code specifically. For some reason if I input "n" I have to press enter twice. But if I input "y", everything works fine and the code moves to the next section. My code is as follows:
do{
try {
if (test) cout << " Re-enter: ";
test = false;
getline(cin, choice);
checkinput(choice);
}
catch (int flag) {
if (flag == 1){ cout << "Error: Input must be y or n."; test = true; }
}
} while (test);
and the checkinput function is as follows:
// function for checking the input of y/n
string checkinput(string c) {
if (c != "Y" && c != "y" && c != "N" && c != "n") {
throw 1;
}
if (cin.fail()) throw 1;
return c;
}
I think you are trying to do too much here. You can simplify this.
There is no need to throw and catch exceptions inside checkinput. Since there is only two cases you can use a boolean. Secondly, you are returning c. I don't know why you are doing that, it isn't being used. You should instead, return a boolean.
checkinput becomes:
bool checkInput(string c) {
if (c.length() > 1)
return false;
return c == "Y" || c == "y" || c == "N" || c == "n";
}
Now you can simplify the do-while and remove the try statement. Additionally, there is no need for the test variable now since we are successfully grabbing any input:
int main() {
string choice = "";
do {
cout << "Enter yes or no (y/n): ";
getline(cin, choice); // or cin >> choice;
bool check = checkInput(choice);
if (!check)
cout << "Error: Input must be y or n." << endl;
} while (true);
}
You may also simplify this further but that will be at the cost of readability. Good luck!
I'm trying to implement a simple game where user is asked for 2 valid integer coordinates between 0 and 10. (int row, int column)
An exemple of what I would realize is:
Insert coordinates: 4C
*Error, number of row and column must be integer
Insert coordinates: 44 2
*Error, number of row or column are too high
Insert coordinates: 4 3
The coordinates you entered are (4,3)
I realized all of these with a do-while cycle.
int r,c;
do{
cout<<"Insert coordinates: ";
cin>>r>>c;
if (cin.fail())
{
cout << "ERROR: Number of row and column must be integer." << endl << endl;
}
if ((r<0 || r>10) || (c<0 || c>10)
{
cout << "*Error, number of row or column are too high [0-10]" << endl << endl;
}
cout<<endl;
}
while (((r<0 || r>10)||(c<0 || c>10)) || cin.fail());
This code doesn't work properly. If I enter 2 numbers between 0 and 10, it works. If I enter a number bigger then 10, it also works. But if I entered a character the program goes into an infinite loop, and does not work properly.
How to implement this to handle errors with character input? Is there a way to recognize, and remain inside the while cycle, if user inputs a character?
If you enter a letter instead of a number, then that letter is not extracted from the input buffer, so your code will continue to fail forever.
If the input fails (why not use e.g. if (!(cin >> r >> c))?) then you can skip the line by doing calling the ignore function:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
You also want to clear the failbit as it's not cleared automatically, this is done with the clear function.
You can also bypass this problem by getting the whole line, and using std::istringstream for the parsing:
do
{
std::string line;
if (!std::getline(std::cin, line))
... // Could not read from input
std::istringstream iss(line);
int r, c;
if (!(iss >> r >> c))
... // Failed to parse as numbers
...
} while (...);
You could simply check if characters were entered, for example:
if (x >= 0x41 && x <= 0x7A)
cout<<"error, you entered a letter";
(((r<0 || r>10)||(c<0 || c>10)) || cin.fail());
change to
(((r>0) && (r<10))||((c>0) && (c<10))) //It will work, no need to check cin.fail();
If cin fails then it might produce errors in buffer so better to quit the program..
The program goes into an infinite loop because you never clear the fail state. You can simplify your entire loop:
#include <iostream>
using namespace std;
int main()
{
int r = -1;
int c = -1;
bool valid = false;
do
{
cout<<"Insert coordinates: ";
if (cin >> r >> c)
{
if (r >= 0 && r <= 10 && c >= 0 && c <= 10)
{
valid = true;
}
}
else
{
cin.clear();
cin.ignore();
}
if (!valid)
{
cout << "ERROR: Number of row and column must be an integer between 0 and 10." << endl;
}
} while (!valid);
cout << "You entered (" << r << ", " << c << ")" << endl;
return 0;
}
I am creating a simple console application that obtain user input which is an integer.
I want the condition so that it should only be an integer and it should be not more than 3 and not less than 0.
The code i came up so far is:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int uinval;
while (true) {
string tempoval;
cout << "Please enter a value between 0-3:\n>";
cin >> tempoval;
stringstream ss(tempoval);
if (ss >> uinval)
{
break;
cout << "Entered an invalid value";
}
while (true)
{
if (uinval < 0 || uinval > 3)
break;
cout << "Value must be between 0-3";
}
cout << "You have entered:" << uinval;
return 0;
}
This works when I input a non-integer value like a,b,c,d. But it does not work when i input -1 or 4 as value.
I am not sure, maybe I confused myself with the while loops.
This is incorrect:
while(true){
if(uinval < 0 || uinval > 3)
break;
cout <<"Value must be between 0-3";
}
You check the condition on uinval repeatedly, without giving user a chance to enter a new value.
To fix the problem, remove the second loop, and replace
if(ss >> uinval) {
break;
}
inside the first loop with
if(ss >> uinval && uinval >= 0 && uinval < 4) {
break;
}
You have your if statement holding the opposite of what you want it to.
Right now if the value is under 0 or over 3, it'll break and continue. You want it to continue if it's over 0 and under 3.
if(uinval > 0 && uinval < 3)
I wish to achieve to limit user on only 0 or 1 when program asking for boolean variable.
I'we tried to do so, but it doesn't work. It still keep asking me for typing in.
bool ele;
do{
cout << "Elektro:\t";
cin >> ele;
if (cin && ele == 0 && ele == 1) break;
cin.clear();
cout << "Neveljaven vnos!" << endl;
}while(true);
The good news is, that operator>> for bool by default allows only '0' or '1' as valid input. That means you don't need to explicitly check the value after read - if stream state is ok, so is your bool:
bool ele;
if (!(cin >> ele)) {
// error;
}
The reason you're getting an infinite loop when you enter something like "cvdsavd" is that you only clear the error flags, but don't get rid of the bad charaters. So your loop keeps trying but never can get a valid input. You need to get rid of the garbage:
bool ele;
while (!(std::cin >> ele)) {
std::cout << "Neveljaven vnos!\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Reference for ignore(). You also need to include <limits> for numeric_limits template.
Lp :)
As chris points out, you want to do if (cin && (ele == 0 || ele == 1)) break;
You cant do that :
if (cin && ele == 0 && ele == 1) break;
because its always false because ele cant be in same time 1 or 0 ... It can be only one of this figures.
if(ele == 0 || ele == 1) break;
Sorry, forgot the code
Here is the incorrect code.
I have been trying to get this working, but all the logical operators do not work.
#include <iostream>
#include <string>
using namespace std;
string repeat;
string repeatnum;
string prompt = "|-[]->";
int main()
{
string entry;
bool Running = true;
while(Running == true)
{
cout << "\n";
cout << prompt;
cin >> entry;
if(entry == "Exit") return 0;
if(entry == "Help") cout << "HELP:\nThsi is a simple program, try an input";
if(entry == "ChangePrompt")
{
cout << "What do you want to change the prompt to?: ";
cin >> prompt;
}
if(entry == "Repeat" || "repeat")
{
cout << "What string do you want to repeat?: ";
cin >> repeat;
cout << "How many times do you want to repeat" << repeat << "(1-9)?: ";
cin >> repeatnum;
if(repeatnum > 0){}
}
}
char f;
cin >> f;
return 0;
}
Here is the error I am getting.
Error:
C:\Users\Packard Bell\Desktop\test\main.cpp||In function 'int main()':|
C:\Users\Packard Bell\Desktop\test\main.cpp|29|error: no match for 'operator>' in 'repeatnum > 0'|
||=== Build finished: 1 errors, 0 warnings ===|
Because at line 29 in main.cpp you attempt to do repeatnum > 0 and repeatnum is a type with no overloaded operator >.
In addition to the repeatnum problem, this piece of code isn't doing what you want
if(entry == "Repeat" || "repeat")
It should be
if(entry == "Repeat" || entry == "repeat")
From the given info, I can only guess that the variable repeatnum is an object of a class or structure which you cannot use to directly compare it with 0. If the type of repeatnum is defined by you, add a member function that overloads operator > and handle it properly.
class YourType
{
// Class definition
public:
int operator >( int var )
{
// Code for comparison
// return result
}
};
Now after seeing the code. repeatnum is a string. You read input to the string and then compare it with integer. Now string does not have operator>-defined for integer so you need to convert the string to integer before comparison.
atoi(repeatnum.c_str());
Or use stringstream to do it.