Hi,
I'm trying to write a loop that lets the user type in a string, and then process that. Previously I've had some problems with strings getting messed up when they are 'reused', that is, when I change it multiple times it has been acting a bit weird (which of course might be just because I'm not a very skilled programmer).
What I have so far is a prompt asking if you want to continue looping or not (enter 1 means 'Yes! Continue this fantastic loop!' and 0 means 'No, please stop this nonsense!'). This part works.
Then after this 'quit-or-continue' prompt, there's yet another, identical prompt that asks if you want to change the string I was talking about earlier.
Now, the problem is that when the code gets to the 'getline(-----)' (line 31) part, it simply passes it without letting me input anything. It's like it's rushing through it all.
I've included my code below here, and then after that an example of how it looks when running.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string.h>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
std::string input1;
std::string input2;
int choice1;
int close=1;
while(close!=0)
{
// INTEGER PROMPT:
cout << "\nEnter 'choice1' (1 or 0): ";
cin >> choice1;
// INPUT:
if(choice1==1)
{
input1.clear();
cout << "\n--- INPUT --- \n String 'input1' reset! Check if empty --> " << input1 << " <--" << endl;
cout << "Enter 'input1': ";
getline(cin,input1);
cout << "Testing new input: ";
// Converting string to char (not important for now)
char * writable = new char[input1.size() + 1];
std::copy(input1.begin(), input1.end(), writable);
writable[input1.size()] = '\0';
for(int i=0;i<input1.size();i++)
cout << writable[i];
}
// FINAL INTEGER PROMPT (CONTINUE):
cout << "Continue? (YES:1, NO:0): ";
cin >> close;
}
return 0;
}
|
1 2 3 4 5 6 7 8 9 10 11
|
Enter 'choice1' (1 or 0): 1
|
String 'input1' reset! Check if empty --> <--
Enter 'input1': Testing new input: Continue? (YES:1, NO:0): 1
Enter 'choice1' (1 or 0): 1
--- INPUT ---
String 'input1' reset! Check if empty --> <--
Enter 'input1': Testing new input: Continue? (YES:1, NO:0): 1
Enter 'choice1' (1 or 0): 0
Continue? (YES:1, NO:0): 0 |
So my question is: is there a way to make it slow down a bit so that I can actually input anything? Or is there a better way to do this?
Thanks!