Okay I need to be able to limit the input parameter to one!!!
Example
Enter a number
1
fine!!
Enter a number
1 2
No good!!
Help please!!!
Well i would like you to be more clear in the question . Do you want a number that is lesser than 10 Or do not want spaces in between while entering.
Because. If there is a space between 2 numbers and you use the command cin>> then it will probarbly take in the first number and omit the second number.
So if you type in "1 2" with the space between them the output will be one itself.
On the other hand if you donot want to have numbers in double digits. You can use if statements to stop the problem.
What i was trying to say is if there is more than one parameter i wanted to be detected and output something like "only one parameter is allowed"?!! if that makes any sense!!!
Here is a copy of what i have done so far
#include <iostream>
#include <limits>
using namespace std;
int main() {
char Greeting[] = "Hello World";
cout << "Hello World\n" <<endl<<endl;
int number = 0;
cout << "Enter an integer: ";
cin >> number;
cin.ignore(numeric_limits<int>::max(), '\n');
if (!cin || cin.gcount() != 1)
cout << "XXXXXXXXXXXXXXXXXXX";
else
{if(number<13)
{
cout << "Crosponding Letter is: " <<Greeting[number]<<endl;
}
else
{
cout << "ZZZZZZ"<<endl;
}
}
return 0;
}
I need to check that the input is one parameter no space in between.
line 18: >>!cin
To my knowlege that can never happen so you might as well delete it.
Otherwise you program seems to work ok for me.
I know this part of the program works but what i need to do is the following when an input is detected with two parameters eg 1 (space) 2 it should be knocked back and cout << "not allowed".
This should give you some ideas how to solve the problem. This just counts the number of inteters until '\n' is detected. You should be able to construct a function that does something similar for what you want.
#include <iostream>
using namespace std;
int main()
{
int n;
int count = 0;
cout << "Enter a number\n";
while(cin.peek() != '\n' && cin >> n)
count++;
cout << "Count = " << count << "\n";
return 0;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.