The script I am working on is over a page long, so I am going to link it (one simple file):
http://pastebin.com/7BVHmQGp
I apologize for that. My problem is I get into an infinite loop in my code, for example after I select 1 or 2 for encrypting/unencrypting it lets me enter the word, and when I next enter the "shift" for the cipher it runs an infinite loop of the menu.
I had tried for so many hours to debug this, I thought it was a problem with cin, for example when you enter an invalid choice it just throws an infinite loop/
What seems to cause the infininte loops?
I think you should be ignoring the newline character instead of a space
I tried with the following and it works on VS2010
cin.ignore(1, '\n');
getline(cin, input);
Try cin.clear, I believe you are constantly reading your initial input character.
cin >> selection;
cin.clear();
This page should explain all you need to know.
Related
I tried to print a line that asks for input from the user, get the input, then print again some line. The problem is that the white space at the end of the first printed line is printed not at the end of the line, but rather at the beginning of second printed line, after i get the input.
I'm completely new to C++ so I couldn't really try much, but i tried printing the code without the part that prompts the input from the user, and it prints the space just fine, but when i add std::cin << input; the space get's sent to the beginning of the second line.
My code:
int input;
std::cout << "Enter your favorite number between 1 and 100: ";
std::cin >> input;
std::cout << "Amazing... That's my favorite number too... wow..." << std::endl;
I want the output to be
Enter your favorite number between 1 and 100: //some input
Amazing... That's my favorite number too... wow...
(note the space before //some input)
Instead i get
Enter your favorite number between 1 and 100://some input
Amazing... That's my favorite number too... wow...
(note the space before Amazing)
Edit: I'm using Clion if it could be connected. Also, I tried to run the executable on powershell and it worked as expected, without the problem, so this has something to do with the Clion terminal. Also, i'm using windows 10 as my OS.
Second Edit: add my findings on my answer.
This seems to be a problem with buffered input of Clion. See this issue: https://youtrack.jetbrains.com/issue/CPP-7437
When you are using CLion, You can try disabling PTY (Help | Find Action > type "Registry" > open Registry > find and disable the run.processes.with.pty option)
CLion moving space into new line
Are you sure about that? I tried both in online shell and on local machine and it works as expected.
After checking I found out that this occurs only on the Clion Run terminal, so this has something to do with it exclusively. I'm currently trying to mess around with the settings. I will post a solution and an explanation here if I find it.
Edit: as mentioned in one comment, it could be the issue mentioned here https://youtrack.jetbrains.com/issue/CPP-7437.
In any case it is a Clion related problem exclusively, and not a C++ problem.
This problem is only applicable for the second line, if you leave that line empty then the problem will be fixed. Don't know about C++ but for C before the print statement you have to add: printf("\n");
What I am trying to do is make a while loop that loops as long as "\n" is not entered. The problem is that it asks for my input once and then just ends. Here is my code
paliTester = cin.get();
while (paliTester != "\n")
{
paliTester = cin.get();
}
The problem is not with your code, the problem is with the console that is providing you with your input. All consoles today provide a line editing option. Meaning you can type a command, erase part of it and rewrite as many times as you want. Until you'll press ENTER no input will be provided to the program waiting to read it. This means there's no way to provide any content to your program without pressing ENTER and ENTER means your program will receive '\n'.
Probably because \n is the new line escape sequence. Use "\\n" instead.
Also try this instead of get().
cin >> paliTester;
while (paliTester != "\\n")
{
cin >> paliTester;
}
I'm a first semester C++ student and in class we are building a BMI calculator (Win32 Console Application). I've gotten everything to work just fine, except for one of the instructions, which is wait for user to press enter to close application.
I had success using the system("PAUSE"); statement but in the past I would declare a string variable, like for example, initialize string genVar; and then use getline(cin, genVar); and when the user pressed Enter, the application would close, but it didnt work this time. The application would simply close. It worked just fine with pause, but not with getline().
Is using getline() for this purpose bad practice? Anyone have any clue why it didn't work?
Thank you in advance!
I know this is an old post, but look at the solution which I posted in this question:
C++ Multiple Program Issues (rand, conversion, crashing)
It explains (as chris mentioned in a comment) that getline tends to leave in a new line character in the buffer, which needs to be cleared and reset. Answer above explains it.
std::cin and std::getline do not mix well in general, you usually want to stick to one or the other in a program to avoid input errors and bugs. Your getline probably is grabbing a left over input from as earlier std::cin. I would recommend using this:
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
you'll need to include <limits> for this to work correctly.
you can also use the getch() function in the place of std::getline() IMHO
I have this block of code form my input class I wish to improve. It works fine at the moment.
http://ideone.com/ryatJ
You use it like this... What it dose is pass a reference to the stream, and a reference to the userinput and a int to mark the max range the input has to be. The usrinput is referenced so we can use that elsewhere and it returns a true false so we can use it like this.. to get input...
while (!(validate(cin, returnval, a))){
//# Error Code code here
}
Basically everything works.. it asks the user to input a number that we are expecting as a int.
Using if (cin >> int) - I get a true false based on if the user entered a int or not. (so any string or anything... errors)
Using > and < we make sure the int is in the correct range, or it errors.
Useing peek we check the stream for a \n.. if it is not found.. it errors. (so 2.2 or 2:3 4#3 etc etc will error)
So that is all working, so well in fact I only just recently noticed that there is one more error check I need. Though I am not sure how to do it.
Basically if you just press enter with out first putting in ANY value, cin just goes to a new line. Is there a way to detect that the user just hit enter and nothing else, so I can return teh correct error message and redraw the menu screen and all the other stuff I wish to do.?
SUMMERY
How can I add to this a function to check if enter is pressed with out entering any other vales?
Thanks for the help
--Jynks
EDIT------
I can not answer my own question.. but this is the answer...
I was very close.. but the answer was in my code already.... meaning that cin.peek() is looking at the stream, at the moment it is calling it after it is read into a variable. This means it is seeing the "2nd" char and why the test works... all I needed to do was make a new test b4 the cin was read and it checked the 1st character..
Solved
http://ideone.com/mt7jH
I mam trying to complete a college assignment in C++ and am having trouble with what should be a very basic operation. I am trying to read a string of characters from the keyboard. This is the relevant code:
string t;
cout << endl << "Enter title to search for: ";
getline(cin, t, '\n');
I understand, that the last line is supposed to read the input buffer (cin , in this instance) and store the character in the 't' string until it reaches a new line character and then continue the program flow.
However, when I run my code in XCode, it just sort of jumps over the getline function and treats 't' as an empty string.
What's going on? I tried using cin >> t but that just read characters forever - Why cant I get this to behave?
The reason that the input operation apparently is skipped, is most probably (that means, ignoring possible peculiarities of a bugsy XCode IDE) that you have performed some input earlier and left a newline in the input buffer.
To fix that, make sure that you have emptied the input buffer after each input operation that logically should consume a line of input.
One easy way is to always use getline into a string, and then use e.g. an istringstream if you want to convert a number specification to number type.
Cheers & hth.,
From the docs page it looks like you want
cin.getline(t,256,'\n');
or something similar.
This sounds like an issue with the way Xcode is running your program. Try running your program directly from the terminal, and see if this is sufficient to fix your issue.