cin

C++ getline method not working

倖福魔咒の 提交于 2019-12-02 13:21:13
问题 I'm sorry but I'm quite new to C++ but not programming in general. So I tried to make a simple encryption/decryption. However when I added the modification to my previous code (so there isn't two programs for encrypting and decrypting) I found that the code 'getline()' method no longer works. Instead it's just ignoring it when the code is ran. Here's the code: int main(){ std::string string; int op = 1; //Either Positive or Negative srand(256); std::cout << "Enter the operation: " << std:

Reading in a specific format with cin

喜夏-厌秋 提交于 2019-12-02 12:32:55
问题 How can i read in a specific format using cin? Example:-for reading a complex number, I would like the user to enter it as usual:x+yi, so i want something like this: cin>>x>>"+">>y>>"i"; But this is giving an error.What is the right way?Help greatly appreciated. 回答1: A very simple solution: char plus,img; double x,y; cin>> x >> plus >> y >> img; if (plus!='+' || img!='i') ...error In "real life" code you build/use a class complex , and overload the operator >>. I try it in Ideone: http:/

Cout of a string is giving an error and a hard time some insight help pls?

末鹿安然 提交于 2019-12-02 12:02:19
I cant find the error in this piece of code could anyone please insight me? I ran the debugging but the errors are un-understandable.. #include "stdafx.h" #include <iostream> using namespace std; int main() { string name; cout << "Input your name please?" << endl; cin >> name; if { (name == "Bart Simpson") cout << "You have been very naughty" << endl; } return 0; } Problems: You have some missing #include s, which probably caused your initial compiler errors. You have a simple syntax error with your if statement. Using the stream extraction operator will never yield a string with whitespace

Limiting number of elements in vector

吃可爱长大的小学妹 提交于 2019-12-02 11:06:44
Trying to limit the amount of inputs that a user could insert into a vector when inputting an array into 1 manually, but for some reason it's being weird. #include <iostream> using namespace std; void fillVector(vector<int>& newThisIsAVector) { cout << "Please type in your 10 numbers separated by a space. On completion press enter."; int input; cin >> input; while (newThisIsAVector.size() < 10) { newThisIsAVector.push_back(input); cin >> input; } cout << endl; } It's supposed to limit you at 10 but instead it's taking 10 then when you press enter it creates a new line. Then you type an 11th

Cin in a while loop

邮差的信 提交于 2019-12-02 10:17:35
问题 So, I've looked around and haven't been able to figure out what's going on with cin during my While loop. I'm working through the book C++ Primer (5th edition) and I noticed that during one of the exercises I couldn't use cin to grab strings without it not terminating the while loop. I fixed this issue by just using getline(). The goal of the current exercise is to ask for user input from the values of 0 - 15, and converting that number into it's "Hex Equivelant" (Where 0 = 1, 1 = 2, 2 = 3, .

C++ alternative to sscanf()

喜夏-厌秋 提交于 2019-12-02 06:50:41
I have the following function: static void cmd_test(char *s) { int d = maxdepth; sscanf(s, "%*s%d", &d); root_search(d); } How can I achieve the same output in a C++ way, rather than using sscanf ? int d = maxdepth; sscanf(s, "%*s%d", &d); Reads a string (which does not store anywhere) and then reads a decimal integer. Using streams it would be: std::string dont_care; int d = maxdepth; std::istringstream stream( s ); stream >> dont_care >> d; 来源: https://stackoverflow.com/questions/7883383/c-alternative-to-sscanf

Check if input is integer

核能气质少年 提交于 2019-12-02 06:21:22
问题 In order to learn C++, I'm translating a program I wrote in Python. I wrote this n = 0 while n < 2: try: n = int(raw_input('Please insert an integer bigger than 1: ')) except ValueError: print 'ERROR!' in order to get an integer bigger than 1 from the user. This is what I wrote in C++ for the moment: int n = 0; while (n < 2) { cout << "Please insert an integer bigger than 1: "; cin >> n; } I took a look at try-catch and it seems pretty straight forward. My concern is about how to check the

Cin loop never terminating

被刻印的时光 ゝ 提交于 2019-12-02 05:03:38
I'm having trouble getting my cin loop to terminate in my program. My program uses Linux redirection to read in input from the file hw07data, the data file look like this: 100 20 50 100 40 -1 A34F 90 15 50 99 32 -1 N12O 80 15 34 90 22 -1 The first portion is the total points for the class, the next lines are the students ID number followed by their scores, all terminated by -1. My issue: my while loop never terminates when i run the command ./a.out < hw07data , could anyone look over my code and give me some hints? I dont want the answer, as this is homework, i just need some guidance. Thanks!

std::cin infinite loop from invalid input [duplicate]

余生长醉 提交于 2019-12-02 05:01:44
问题 This question already has answers here : Infinite loop with cin when typing string while a number is expected (4 answers) Closed 3 years ago . Please read the following code: #include <iostream> #include <cstdlib> int main() { std::cout << "Please input one integer." << std::endl; int i; while (true) { std::cin >> i; if (std::cin) { std::cout << "i = " << i << std::endl; break; } else { std::cout << "Error. Please try again."<< std::endl; std::cin.ignore(); std::cin.clear(); } } std::cout <<

Is there a way to take a series of zeros as an int input? [duplicate]

那年仲夏 提交于 2019-12-02 04:42:40
This question already has an answer here: Find the reverse of a number (ex : 2500 reverse 0025) without the help of string or character 4 answers I'm in the process of recreating the old mastermind video game from the '70s. The users makes a four digit guess, which is then logged as a vector. I know there exists a way to accept the input as a string, however I would find it odd if there isn't a way to do this with one single int input -- which I haven't been able to find a method for. The way I have it currently set up, if the user provides an input of 0003, this will be logged as an