Problem with switch

Hi guys,

This is my first post here... and my first (non-hello world) program of any kind. I'm rushed to learn C++ as physics grad school might be on the horizon and I didn't bother to learn any programming as an undergrad :/.

I'm about a week into U of W's open course ware for csc 101/102 (implemented in C++), which is quite outdated. So I have almost certainly picked up some bad habits.

This program is designed to act like a university book store. It starts up and the user loads in the book catalog ("list.txt"). The user is then prompted for a subject which is then found in the catalog. The user can both buy and return multiple copies.

My problem is that in the default case of the switch at the end of main(), in which the input is supposed to be int, misbehaves if the user gives it a char. If a non defined integer is entered, the appropriate error occurs, but if its a char then it constantly loops through the buying cycle picking a random book and continuously adding it to the balance.

I would also love any kind of constructive feedback as to how to best progress learning from here. I have a copy of C++ primer 5th ed which I plan on having as my next C++ book and Intro to algorithms by cormen as my next theoretical one.

Let me know what you think of it as a first program.

Thanks!
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 <fstream>
#include "conio.h"
#include <stdio.h>
#include <string>
#include <stdlib.h>

using namespace std;
ifstream input;
string mslist[100];
string msplist[100];
double unitcost;
string subject;

void openfile(void)
{
	char catalog [100];
	cout << "Enter catalog name (list.txt): ";
	cin.getline(catalog, 100, '\n');
	input.open(catalog, ios::in);
	if (!input.good()){
		cout<< "cant open file"<<endl;
		exit(1);}
}

void makearrays()
{
	string subj;
	string price;
	int i=0;

	for (i=0;!input.eof(); i++){
	getline (input, subj, ' ');
	mslist[i]=subj;
	
	getline (input, price, '\n');
	msplist[i] = price;}
}

double cost()
{
for(int i=0; i<100; i++)
	{if (subject == mslist[i])
	{unitcost = atof(msplist[i].c_str());}
	}
return unitcost;
}

int main()
{
	int command = 1;
	int number = 0;
	double balance = 0;
	
	openfile();
	makearrays();
	
	while(command != 3) 
	{
	
	cout << "Welcome to the bookstore. Would you like to buy (1) , return (2) , or quit (3)?"<<endl;
	cout << "Your balance is: "<< balance << endl;
	cin >> command;
	switch(command)
		{case 1:
			cout << "Which subject are you interested in?"<<endl;
			cin >> subject;
			cost();
			cout << "The price per book is:" << unitcost <<endl;
			cout << "How many copies would you like?"<< endl;
			cin >> number;
			balance += number*unitcost;
			break;
		case 2:
			cout << "Which subject are you interested in?"<<endl;
			cin >> subject;
			cost();
			cout << "The price per book is:" << unitcost <<endl;
			cout << "How many copies would you like to sell?"<< endl;
			cin >> number;
			balance -= number*unitcost;
			break;
		default:
			cout << "Invalid command" <<endl;
			break;
	}
	}
}


I'm not sure how to upload a .txt file so here are the contents:
physics 129.99
chemistry 99.99
computer science 117.99
math 89.99
engineering 119.99
english 49.99
history 74.99
in which the input is supposed to be int, misbehaves if the user gives it a char.


You can make the command variable a char. In the cases use single quotes as in '1', '2', 'q'.

Hope all goes well.
Ideasman,

Thanks. That solved the problem but revealed other one. Now lets say the user just hit a bunch of keys when prompted to buy sell or quit. For an example they input "nksw." The switch would run through once for n, k, s and w separately so the terminal would get 4 errors. I thought of making the command a string to solve this, but visual studios said that wasn't allowed...

Second question. I wanted to have a "case 'q': exit(1); break;" in my switch and leave the while loop without an expression, which I know I have done before. But again visual studios didn't like that. Any thoughts?
If the user enters "nksw" and a char is expected, it will just take the 'n', and act accordingly.

2nd question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool Quit = false;
while (!Quit){
....

switch (input) {

....

    case 'q':
          Quit = true;
           break;
     default:
......
}
}


This way execution can continue after the switch without bombing out altogether.

Good luck !!
Last edited on
Topic archived. No new replies allowed.