I want program to accept numbers only [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want my program to accept numbers only between 1 and 4 as input. And if input is an alphabet or or any other number instead of 1, 2, 3 or 4, then it should show an error and prompt the sir user again to input correct values. This is what i'm doing right now.
if (x < 1 || x > 4)
{
cout << "Invalid input!";
}
else if (x == 1)
{
// rest of the program
}

do something like this
unsigned char x;
int num=0;
input:
num =0;
cout<<"Enter input value"<<endl;
cin>>x;
num = x - 48;
if(num>4||num<1)
{
//enter again
goto input;
}

Related

How to display all even number that are smaller or equal to the number inserted [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 months ago.
Improve this question
How to create a code in c++ that if I insert any number "n" it displays all the "even" numbers that are smaller or equal to "n"
I dont have much knowledge in C++ so I would appreciate the help
This is what I tried and think it's good enough
int n;
cout << "\nEnter number= ";
cin >> n;
cout << "\nEven numbers are ";
for(int i = 0; i <= n; i++)
{
if ( i % 2 == 0 )
{
cout << i <<" ";
}
}
You can use a repetition loop to check for all the numbers until N and the look if each number is divisible by 2 (when, in that division, the remainder is 0).
#john ideia of starting counting at 2 and going up 2 at a time is a easier one.

How can I assign the integers in an input like 1.2.3 to variables in C++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
Here is an example of what I mean.
Input: 10.20.50
a = 10
b = 20
c = 50
you will need to store the input in a string or char[] and then iterate over the string or char[] and write some code that will identify the separate parts of the input and convert them to ints using stoi().
this would work but just an example (and i think it will not print the final number unless the input is ended with "." but this should give you an idea of what you could do.
std::string i = "";
std::cin >> i;
std::string buffer = "";
for (auto c : i)
{
if (c != '.')
{
buffer += c;
}
else
{
int num = std::stoi(buffer);
buffer = "";
std::cout << num << ", ";
}
}

how to define int number using comparison operators [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to declare
int x = it must be more or equal to 1 but less or equal to 100;
How can I do it? I dont want to use if condition, Im looking for something short and clear, if possible.
The x number is input, so program should accept only numbers in this limit.
It seems that you're looking to error check on initialization.
If I were you I'd do something along the lines of.
int x;
cout << "Enter a value: " << flush;
cin >> x;
while(!((x>=1)&&(x<=100))) {
cout << "Try Again: " << flush;
cin >> x;
}

Stuck in C++ data structure program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a program that prompts the user for a string and uses a recursive function to print the string backward. Do not use any global variables; use the appropriate parameters. Could you give me some hints, like pseudocode?
int stringbackwards(string a){
if()
else
}
int main(){
string name;
cout<<"Write a name: ";
cin>>name;
cout<<"Backwards "<<stringbackwards(name);
return 0;
}
Why do you use a recursion for that?
There is a good concept in c++ called iterators that already has this functionality implementet :)
http://www.cplusplus.com/reference/string/string/rbegin/
So in your case:
cout<<"Backwards ";
for (std::string::reverse_iterator rit=name.rbegin(); rit!=name.rend(); ++rit)
{
cout << *rit;
}
But to make it recursive, i would do it like this (Pseudocode).
function backwards(std::string& name, unsigned int length)
{
unsigned int length = name.length();
unsigned int currLength = length - 1;
if (length > 0)
{
backwards(name, currLength);
}
std::cout << name[length - currLength - 1];
}
Hint:
Say the string is "abcd". You would like to print "dcba". In other words, you are printing the last letter first.
Hence you will first go deep into recursion and then after coming back, print the letter 'a'.

How to exclude space when reading input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to read an input by user.
Example: If user inputs 233 245 (consider the space).
i need to assign it to variable like
a=233;
b=245;
How do i do this in c++?
You can do following:
#include <iostream>
int main(int argc, char **argv)
{
int a,b;
// Get values
std::cin >> a >> b;
// Print out values
std::cout << a << ' ' << b << '\n';
}
This reads user input from standard input to the variables a and b, and then prints them to standard output