Help with "easy" program.

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
/ Fields.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{	
	string name[10];
	int i = 0;
	cout << "\n***************";
	cout << "\n** Welcome **";
	cout << "\n****************\n";
	cout << "Enter 10 names\n";
	cout << "**********MENU********\n";
	cout << "1. Enter another name\n";
	cout << "2. Show names\n";
	char choice;
	cout << "Choose: ";
	cin >> choice;
	switch(choice){
	case '1':{
	int i = 0;
	int svar = 1;
		while(i < 10){
			cout << "Person " << i+1 << ": ";
			getline(cin, name[i]);
			cin.ignore(1000, '\n');
	cout << "Continue?";
	cin >> svar;
	if(svar == 1){
		i++;
	}
	else{
		i = 10;
	}
		}
			 }
		 
	case '2':{
	cout << "***************************************\n";
	cout << "These are the names:\n ";
	int pos = name[i].find(' ', 0);
		string fName = name[i].substr(0, pos);
		int start = pos+1;
		int langd = name[i].length()-start;
		string eName = name[i].substr(start, langd);
		cout << eName << ", " << fName << "\n";
	
	
}
	}
	

	return 0;
}


This program should allow me to enter 10 names then have them presented like this: Levengood, Mark
Fishtown, Paul
etc. But the problem is I write the names but they don't "save" so to speak, like name[i] doesn't exist. Appreciate help. And as im posting this in this section of the forum I think you already expect that I am a beginner.
Last edited on
closed account (D80DSL3A)
I see one problem. By the time case 2 is reached i=10 and there is no loop there. The code is working on name[10] only, which is outside of the array bounds.
You need a for or while loop in case 2 so you can loop through all of the names that were entered in case 1.
What if fewer than 10 names are entered? Line 37 assigns i=10 regardless of how many names were entered. I think a break; there would get you out of the while loop but preserve the value of i for use as the upper limit in the loop for case 2.
There may be other problems but your method of placing braces makes it too hard for me to see what blocks of code are enclosed within which braces.
Hope this helps.
for(int n = 0; n < i; n++) could that loop work in case 2?

edit: still doesn't work though, now the console just shuts down when it gets to case 2, it's like it doesn't find the names. It writes 10 ","on 10 lines but no names. I removed the braces after "case 'x':" btw, don't know if those needs to be there.
Last edited on
closed account (D80DSL3A)
Yes, that's the kind of loop I was thinking of. Did you change the array indexes from i to n for the code within the loop?
Can we see the new code?
In case '1', you need to put the call to cin.ignore before the call to getline. Basically, your input from cin >> choice; and cin >> svar; are leaving newlines in the stream. Then, when you call getline, it reads up to the first newline (so it reads nothing!). Putting cin.ignore before getline will remove the newlines, thus allowing getline to read in the name correctly.
/
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
/ Fields.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{	
	int i = 0;
	string name[10];
	cout << "\n***************";
	cout << "\n** Welcome **";
	cout << "\n****************\n";
	cout << "Enter 10 names\n";
	cout << "**********MENU********\n";
	cout << "1. Enter another name\n";
	cout << "2. Show names\n";
	char choice;
	cout << "Choose: ";
	cin >> choice;
		switch(choice){
		case '1':
	while(i < 10){
	int answer=1;
	cout << "Person " << i+1 << ": ";
	cin.ignore(1000, '\n');
	getline(cin, name[i]);
	cout << "Continue?(1=yes)";
	cin >> answer;
			if(answer == 1){
			i++;
			}
	else{
		break;
	}
	}
		 
	case '2':
	cout << "***************************************\n";
	cout << "These are the names:\n ";
		for(int n = 0; n < i; n++){
		int pos = name[i].find(' ', 0);
		string fName = name[i].substr(0, pos);
		int start = pos+1;
		int langd = name[i].length()-start;
		string eName = name[i].substr(start, langd);
		cout << eName << ", " << fName << "\n";
		}
	
			 
		}

	return 0;
}


This is the new code, it actually works "better" now. If I for example write 3 names then stop it writes the last name i put in 2 times in the xxxx, yyyy form. I guess I got the loop in case 2 wrong, probably missing the obvious as its been a long day and im rather tired atm.
Replace all the i's with n's in the body of the loop.

EDIT: Also, the reason it only outputs two names instead of three is because you still need to increment i before you break from the while loop.
Last edited on
closed account (zb0S216C)
In you while loop, answer is pushed and popped with each pass. It's best to declare answer before the loop.

Wazzak
Thanks alot guys, I got it to work now. :)
Topic archived. No new replies allowed.