Overclock.net banner

1 - 15 of 15 Posts

·
Registered
Joined
·
3,848 Posts
Discussion Starter · #1 ·
Alrighty, so I'm taking my first class in C++. It's fun, but it's got me a bit stumped right now.

Basically, I'm making a program that will scramble a 4 letter word, keeping the first letter the same. However, I want the loop to wait for the user to press enter, and then loop, or quit when 'q' is typed.

There are a couple rules to this assignment:
-I can't use global variables
-I have to use an array, no strings
-Probably shouldn't use any outside the scope of what we've learned already, which is pretty basic I/O functions from the iostream package in the std namespace.

Basically, what I'm thinking is something along the lines of this:

Code:

Code:
char quit;
quit = 0; //Beginning value.

while(quit != 'q'){
   *word scramble code here*;

   cin >> quit;
   cin.ignore(100,'\n'); //To clear the input buffer for next loop.
}
I can get the program to quit just fine when I press 'q' then enter, but I can't get the program to continue otherwise. Every time I press enter it just brings the cursor down. However, if I type any other character than 'q', and then press enter, the loop goes again just fine.

I understand what's happening, it's just a line return, but why isn't the input buffer taking it as a blank entry? Is there a way to make it so that input buffer records the '\n' as opposed to simply acting on it?
 

·
Registered
Joined
·
817 Posts
are you saying that pressing enter breaks you out of the while loop?

cin automatically ignores newline characters so it will never be stored in quit. if you want to record the whitespace, you can use getline. there's a nice article here if you want to read up on the differences between cin and getline.
 

·
Registered
Joined
·
3,848 Posts
Discussion Starter · #3 ·
I ended up just prompting the user to type 'n' and press enter to see the next scramble, however I'm still curious if there is a way to accomplish what I was trying to do. I wanted to submit the assignment, since that portion of it wasn't part of the grade anyway.

How can I continue a loop on enter, or quit a loop on key press 'q'?
 

·
Premium Member
Joined
·
2,146 Posts
Code:

Code:
char quit;
quit = 0; //Beginning value.

while(quit != 'q'){
   *word scramble code here*;

   //cin >> quit;
   getline(cin, quit);
   cin.ignore(100,'\n'); //To clear the input buffer for next loop.
}
 

·
Registered
Joined
·
3,848 Posts
Discussion Starter · #6 ·
Looks like we posted at the same time.

Ahhh getline was exactly what I was looking for! It's funny, but now that you show me that, I vaguely remember it being mentioned in class. I'm not sure she expected us to use it just yet though.

Thanks for the article!
 

·
Registered
Joined
·
1,231 Posts
i did this real quick...test it for me.

Code:

Code:
#include <iostream>

using namespace std;

int main(){

char quit;
char word [100];
char scrambled_word [100];

cout << "Enter a word to be scrambled: ";
cin >> word;
int length = strlen(word);

for(int j = 0; j < strlen(word); ++j)
scrambled_word[j] = word[j];

scrambled_word[length] = '\0';
word[length] = '\0';

for(int i = 1; i < strlen(word); ++i) {

if(i == strlen(word) - 1)
break;

char holder = word[i];
char holder2 = word[length - 1];

   scrambled_word[i] = holder2;
   scrambled_word[length - 1] = holder;

   cout << "Hit 'q' to quit, or 'c' to continue program: ";
   cin >> quit;

   if(quit == 'q')
   break;
   cout << "Program continued...word being scrambled!" << endl;
   --length;
}
cout << "------------Scrambling complete---------------" << endl;
cout << "Original word is: " << word << endl;
cout << "Scrambled word is: " << scrambled_word << endl;
return 0;
}
 

·
Registered
Joined
·
3,848 Posts
Discussion Starter · #9 ·
Whoa, this thread got bombarded hit all at once!
smile.gif


Thanks you guys, +rep.

surfbomb, I'm looking at your program there, and it might be a bit over my head at this point. Does strlen(word) set the length of the string? Or is that just a way to get a value of the size of the array?

EDIT:
Testing it, one moment.
 

·
Registered
Joined
·
1,231 Posts
Quote:
Originally Posted by SectorNine50;15346430
Whoa, this thread got bombarded hit all at once!
smile.gif


Thanks you guys, +rep.

surfbomb, I'm looking at your program there, and it might be a bit over my head at this point. Does strlen(word) set the length of the string? Or is that just a way to get a value of the size of the array?

EDIT:
Testing it, one moment.
strlen simply returns the length of the char array...so the word "red" will return 3. If you have questions on it let me know.

I'm simpling swapping like this:

river -> rrvei

rrvei -> rrevi
 

·
Registered
Joined
·
3,848 Posts
Discussion Starter · #11 ·
Quote:
Originally Posted by surfbumb;15346443
strlen simply returns the length of the char array...so the word "red" will return 3.
Very handy... Thank you! I limited mine to only 4 letters for simplicity sake.

It looks like the loops was misplaced in your program. I was working on making it scramble differently each time it looped. Much cleaner way of doing it than what I did by the looks of it though:

Code:

Code:
Enter a word to be scrambled: test
Hit 'q' to quit, or 'c' to continue program: c
Program continued...word being scrambled!
Hit 'q' to quit, or 'c' to continue program: c
Program continued...word being scrambled!
------------Scrambling complete---------------
Original word is: test
Scrambled word is: ttse
 

·
Registered
Joined
·
1,231 Posts
Quote:
Originally Posted by SectorNine50;15346479
Very handy... Thank you! I limited mine to only 4 letters for simplicity sake.

It looks like the loops was misplaced in your program. I was working on making it scramble differently each time it looped. Much cleaner way of doing it than what I did by the looks of it though:

Code:

Code:
Enter a word to be scrambled: test
Hit 'q' to quit, or 'c' to continue program: c
Program continued...word being scrambled!
Hit 'q' to quit, or 'c' to continue program: c
Program continued...word being scrambled!
------------Scrambling complete---------------
Original word is: test
Scrambled word is: ttse
I didn't know it had to be randomly scrambled...for random number generation use:

random number between 0 - 3

Code:

Code:
int random_num = rand() % 4;
 

·
Registered
Joined
·
3,848 Posts
Discussion Starter · #13 ·
Is there a way to make sure that the rand() function won't draw the same number twice?
 

·
Registered
Joined
·
1,231 Posts
here is an updated version...sometimes small words are scrambled as the exact same before the scramble...so I implemented that real quick.

Code:

Code:
#include <iostream>
#include <ctime>

using namespace std;

int main(){

char quit;
char word [100];
char scrambled_word [100];

cout << "Enter a word to be scrambled: ";
cin >> word;
int length = strlen(word);

for(int j = 0; j < strlen(word); ++j)
scrambled_word[j] = word[j];

scrambled_word[length] = '\0';
word[length] = '\0';

int random_num = 0;
int random_num2 = 0;

srand((unsigned)time(0));

jump:

for(int i = 1; i < strlen(word); ++i) {

if(i == strlen(word) - 1)
break;

while(random_num == 0)
random_num = rand() % (strlen(word));

while(random_num2 == random_num || random_num2 == 0)
random_num2 = rand() % (strlen(word));

char holder = scrambled_word[random_num];
char holder2 = scrambled_word[random_num2];

   scrambled_word[random_num] = holder2;
   scrambled_word[random_num2] = holder;

   random_num = 0;
random_num2 = 0;

   cout << "Hit 'q' to quit, or 'c' to continue program: ";
   cin >> quit;

   if(quit == 'q')
   break;
   cout << "Program continued...word being scrambled!" << endl;
   --length;
}
if(strcmp(scrambled_word, word) == 0)
goto jump;

cout << "------------Scrambling complete---------------" << endl;
cout << "Original word is: " << word << endl;
cout << "Scrambled word is: " << scrambled_word << endl;

return 0;
}
 
1 - 15 of 15 Posts
Top