loop until user choose to exit

Hello all,
This is my first post on this forum. I like this forum a lot. My go-to place when i need help with c++.

I am trying to write this program that will repeatedly asks for and gets a string from the user and presents the following menu of operations on the string until user chooses to exit.
My problem is that the program will not ask the user to input a string again after the first initial input.
I need the program to start over fresh from the beginning until user input 7.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

void searchString(char[], char);
void modString(char[], int, char);
void firstDisplay(char[], unsigned int);
void lastDisplay(char[], unsigned int);
void first_lastDisplay(char[], unsigned int, unsigned int);
void null_Str(char[]);

int main()
{
	
	int index_position, first_index, last_index;
	char selection, key;
	char str[50];
	
      do{

	cout << "Please enter a string: ";
	cin.getline(str, 50);
	
	cout << "Your string reads: " << str << endl;
	
	cout << "\nMake your selection: ";
	cout << "\n1. Search for a character in the string";
	cout << "\n2. Change a character within the string";
	cout << "\n3. Display the first n characters of the string";
	cout << "\n4. Display the last n character of the string";
	cout << "\n5. Display all characters that lie between two given indices";
	cout << "\n6. Null the string";
	cout << "\n7. Exit";
	cout << "\nYour selection: ";
	cin >> selection;
	

	if (selection == '1')
	{
		cout << "\nPlease enter a character to serach for: ";
		cin >> key;
		searchString(str, key);
	}
	else if (selection == '2')
	{
		cout << "\nWhat is the index of the character you want to chage? ";
		cin >> index_position;
		cout << "What character do you want in that position? ";
		cin >> key;
		modString(str, index_position, key); 
	}
	else if (selection == '3')
	{
		cout << "\nHow many characters from the beginning of the string do you want to display? ";
		cin >> first_index;
		firstDisplay(str, first_index);
	}

	else if (selection == '4')
	{ 
		cout << "\nHow many characters from the end of the string do you want to display? ";
		cin >> last_index;
		lastDisplay(str, last_index);
	}
	else if (selection == '5')
	{
		cout << "\nPlease enter the beginning index: ";
		cin >> first_index;
		cout << "\nPlease enter the end index: ";
		cin >> last_index;
		first_lastDisplay(str, first_index, last_index);	
	}
	else if (selection == '6')
	{
		null_Str(str);
	}
        }while (selection != '7');

	if (selection == '7') 
	cout << "\nPress any key to continue.";
	_getch();
	return 0;

}
void functions below but i don't think it will be necessary.
  

what am i doing wrong?

Thank You for all the help.
a) i wouldnt use conio.h
b) i wouldnt use using namespace anything; (with a few exceptions)
c) why are you including string if you dont use std::string?
d) why are you using char arrays over std::string?
e) theres no point in the if statement testing if selection == 7, because there is no way to break out of the loop otherwise.
f) could you post your whole code, because im thinking your caught in an infite loop somewhere, as i dont see any reason that will stop it
Few thing:

Don't use character arrays when you go so far as to #include <string>. For that matter, don't use conio.h at all- no need, since you have strings. Not to mention that you can use this line of code:

std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

in place of _getch() for something that isn't... well, there's a forum thread on it.


Lengthy if-else chains using == to explicit values can be replaced with switch statements- they tend to work far more efficiently, and look nicer.


As for why your code doesn't work, try putting this:

cin.ignore(10000, '\n')

before the getline call. You see, using cin << inputs everything into the correct variable, but never actually clears out the newline when you press enter. The call to getline terminates when it reaches the newline, causing it to be jumped. What cin.ignore does is ignore everything in the input buffer up until a newline, then jump to right after that. That way, the getline won't get an unnecessary newline when the code loops back around.
thank you very much for all your comments and inputs.

@Little Bobby Tables (802)
b) using namespace is how i was taught in class.
c) deleted that. you r right i don't need that. i put it there at the beginning of my code and totally forgot about it.
d)im a noob :)
e)fixed that too
f)it's my project for school and if my professor find out i can get in trouble for posing the whole code online.
thank you again for ur comments

------------------
@Ispil
i got it fixed using cin.ignore. Thank you very much.
im not good with switch but i will try to write another one using switch statements.


Topic archived. No new replies allowed.