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
Related
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 7 months ago.
Improve this question
#include <iostream>
#include <string>
int main(){
int a, b;
std::cin >> a;
std::cin.ignore(3, '/');
std::cin >> b;
std::cout << a << " " << b;
return 0;
}
How can I change my code so that the separators do not have to be Slash characters “/” but any separator that is not an integer number.
You can't use ignore for that.
Instead use a loop to peek at and fetch the next character until the character matches your condition to stop "ignoring" character.
The characters you read that you want to "ignore", just don't do anything with them.
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 1 year ago.
Improve this question
I need to make a program that will write A+B if the symbol is +, A-B if the symbol is -, but I dont know how to declare a variable that is + or -. Thanks in advance!
strangely enough I can't find a proper duplicate.
#include <iostream>
int main(int argc, char **argv)
{
char c;
std::cin >> c;
std::cout << "this is the char I got: " << c << "\n";
return 0;
}
compile your program with
$ g++ main.cpp -o main
and then run it:
$ ./main
+
this is the char I got: +
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 6 years ago.
Improve this question
I used cin.getline after cin.ignore() but I am getting an error saying unassigned int... Not sure what to do or what is wrong. Any suggestions?
Here is my code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string phras;
cout << " Provide a phrase, up to 30 characters with spaces > " << endl;
cin.ignore();
cin.getline(phras, sizeof(phras));
cout << " The phrase is: " << phras << endl;
cout << endl;
return 0;
}
UPDATE
I changed cin.getline(phras, sizeof(phras));
to getline(cin,phras)
Problem solved! Thanks for the help everyone!
The problem is that
char letter[1];
isn't large enough. If a C string is to hold up to N characters, it needs to be declared char letter[N+1] to allow room for the null terminator character. So if the user is going to type a single character, it needs to be:
char letter[2];
As a result, you're getting undefined behavior when cin >> letter writes 2 characters into the array that only has room for 1.
Similarly, if the user is allowed to type a 10-letter word, it should be:
char word[11];
and it should be:
char phrase[31];
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'.
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;
}