my switch statement doesnt work

im trying to make my switch statement work but when i press y it skips case 1 code.

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
89
90
91
92
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

void personCreator();
void extraInfo();

int main()
{

	for (;;)
	{
		system("cls");
		personCreator();
		cout << "Press Enter to continue" << endl;
		cin.get();

	}
}

void personCreator()
{
	string firstname, lastname, birthmonth, birthyear, date, extraInfo;
	cout << "Whats your firstname? "; getline(cin, firstname);
	cout << "Whats your lastname? "; getline(cin, lastname);
	cout << "Whats your birth year? "; getline(cin, birthyear);
	cout << "Whats your birth month? "; getline(cin, birthmonth);
	cout << "Whats your birth date? "; getline(cin, date);
	cout << "Got anything else to say about yourself?" << endl;
	cout << "y for yes, n for no." << endl;

	char response;
	cin >> response;
	switch (response)
	{
	case 'y':
	{
		fstream moreInfo(firstname + "_" + lastname + ".txt", std::fstream::out || std::fstream::app);
		if (moreInfo.is_open())
		{
			system("cls");
			cout << "Type in your extra info"; 
			cin, extraInfo;
			moreInfo << extraInfo << endl;
		}
		else
		{
			cout << "error" << endl;
		}
	}

	case 'n':
		break;
	default:
		cout << "Invalid choice" << endl;
	}

	ifstream reader(firstname + "_" + lastname + ".txt");
	if (reader.is_open())
	{
		system("cls");
		reader >> firstname >> lastname;
		int agey = 2015 - atoi(birthyear.c_str());
		cout << "Welcome back " << firstname << " " << lastname << " who is " << agey << " years of age." << endl;
	}
	else
	{
		system("cls");
		cout << "Success, thank you for registering with us." << endl;
	}

	ofstream myfile;
	myfile.open(firstname + "_" + lastname + ".txt");
	if (myfile.is_open())
	{
		myfile << firstname << " " << lastname << endl;
		myfile << birthyear << endl;
		myfile << birthmonth << endl;
		myfile << date;
		myfile.close();
	}
	else
	{
		system("cls");
		cout << "Error" << endl;
	}
}

Last edited on
The two items that stand out to me is the set of braces in your case 'y' code as well as you are missing a break statement at the end of the case 'y'. I'm not positive that the braces affect the flow of control through the switch statement, but you definitely need a break statement at the end of the case. I personally have never used braces within a switch statement. Hope that helps.
On line 41 use bitwise-OR instead of logical-OR ( i.e. 1 pipe char | instead of 2 pipe chars || )
On line 46 get input like you did for everything else: getline(cin, extraInfo);
@booradley60 getline(cin, extraInfo); causes the program to skip the userinput what do i do then?
getline(cin, extraInfo); causes the program to skip the userinput

That's because line 36 is reading a single y or n character, but NOT reading the newline. Then line 46 reads the rest of the line, which contains nothing.

I suggest that you read the Y/N input as a string and check it with an if/then/else. You can't use switch() with strings, only integral types.
Topic archived. No new replies allowed.