need help

i need to do interactive program
that have 3 option
1-entter string and function will print the first letter from each line
if string is empty print "empty string"(i haee problem with this function) please help.
2-i still thinking how to do it
3-exit the program
all the program in endless loop.


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
 #include<iostream>
#include<string>
using namespace std;
void func1(char*str);
void main(){
	bool flag = true;
	int option;
		char* str = new char[100];
		while (flag){
			cout << "1-for option 1" << endl << "2-for option 2" << endl << "0-to exit" << endl;

			cin >> option;
			cin.ignore();
			switch (option){
			case 0:
				exit(0);
				break;
			case 1:
				cout << "Please Enter a string" << endl;
				cin.getline(str, 99);

				func1(str);
				break;

			}
			system("pause");
		}
}
void func1(char *str){
	int times = 0;
	for (int i = 0; i < 100; i++){
		if (str[i] != '0'){
			times++;
		}
	}
	if (times == 0)
		cout << "empty string"<< endl;
	else
		cout << str[0];
	for (int i = 0; i < 100; i++){
		if (str[i] == ' '){
			cout << str[i + 1];

		}
	
	}
		
	



}
i got it sorry for the interrupt guys;

void func1(char *str){
int len = strlen(str);

if (len == 0)
cout << "empty string"<< endl;
else
cout << str[0];
for (int i = 0; i < 100; i++){
if (str[i] == ' '){
cout << str[i + 1];

}

}





}
guys how to delete the string in option 1
beacuse when i press option 1 and write adi was here
its print awh
and then i want to do function 1 again and put empty string
its print "empty string" and still print awh

i tried delete []str but still problem happens
Hello ebii,

The line void main() should be int main(). void is no longer an accepted way of writing line 5. Also just before the closing brace of main you need the line return 0;.

You have included the header file <string>, but on line 8 you are using a char array when all you need is std::string str{ "" };. The { "" } initializes the string to an empty string and you should always initialize your variables.

Case 0: should not exit(0):, but set flag to false exit the switch then exit the while loop to end the program.

Line 26 would work better after line 22.

Not sure how you arrived at a value for "len", but using strings the if statement could simply say:
if (str.length() == 0).

Hope that helps

Andy
Last edited on
tnx andy for the tiips mate i fixed the program now.
iam trying still to solve the problems i will try hard if i cant get answer i will post again

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
#include<iostream>
using namespace std;
void func1(char* str);
void func2(char *str);
int main(){
	bool flag = true;
	int option;
	while (flag){       //reapet itself untill flag=false
		cout << "1-for option 1" << endl << "2-for option 2" << endl << "0-to exit" << endl;
		cin >> option;
		cin.ignore();
		switch (option){
		case 0:
			flag = false;
			break;
		case 1:
			char* str = new char[100];
			cout << "Please Enter a string" << endl;
			cin.getline(str, 99);

			func1(str);
			break;
		case 2:
			char* str = new char[100];
			cout<<"Enter Id"<<endl;
			cin.getline(str, 99);
			func2(str);

			break;
		}
		system("pause");
	}              //end switch
	return 0;
}          //end while
void func1(char *str){
	int len = strlen(str);

	if (len == 0)
		cout << "empty string" << endl;
	else
		cout << str[0];
	for (int i = 0; i < 100; i++){
		if (str[i] == ' '){
			cout << str[i + 1];

		}
	

	}
	delete[] str;



}
void func2(char *str){

}


for some reason can accses to switch case 2 option any idea why
Last edited on
Hello ebii,

Lines 17 and 24 should not be there. Only one is need above the while loop. If you do not receive a redeclaration error because you are trying to redeclare the same variable each time through the while loop it will likely cause other problems. One of which is not reaching case 2.

Have you learned about std::strings yet? Much better to use than a pointer to a C style char array.

Hope that helps,

Andy

Edit: I found that line 17 should be moved to the top of the while loop say line 9 to work with func1 properly. And the system("pause"); should be put before each break statement.
Last edited on
Topic archived. No new replies allowed.