Help: Code doesn't work the way I want it to

Hi. I have a problem with my coding but I don't know why. When I debug the program, it keeps showing warning C4700, and when I typed in a letter for switch, it shows default unlimitedly.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	cout << "Welcome to BOOKAZINEWEB!";
	cout << "Books Available in the BOOKAZINE WEB";
	cout << setw(3) << "\n A (Ladybird - $15.98)";
	cout << setw(3) << "\n B (Sidney Sheldon - $25.50)";
	cout << setw(3) << "\n C (Nora Roberts - $28.45)";
	cout << setw(3) << "\n D (Penguin Classics - $40.55)";
	cout << setw(3) << "\n E (Virginia Andrews - $42.98)";
	
	int counter =0;
	int quantity;
	
	int book1;
	int book2;
	int book3;
	int book4;
	int book5;

	double price1 = 15.98;
	double price2 = 25.50;
	double price3 = 28.45;
	double price4 = 40.55;
	double price5 = 42.98;

	while (counter != -1 )
	{

	int choice;

	cout << "\nPlease input the letter based on the book type (-1 to checkout)";
	cin >> choice;

	switch (choice)
	{
		case 'a':
		case 'A':
			cout << "Quantity for'Ladybird'?\n";
			cin >> quantity;
			book1 *= quantity;
			price1 *= quantity;
			break;

		case 'b':
		case 'B':
			cout << "Quantity for 'Sidney Sheldon'?";
			cin >> quantity;
			book2 *= quantity;
			price2 *= quantity;
			break;
			
		case 'c':
		case 'C':
			cout << "Quantity for 'Nora Roberts'?";
			cin >> quantity;
			book3 *= quantity;
			price3 *= quantity;
			break;

		case 'd':
		case 'D':
			cout << "Quantity for 'Penguin Classics'?";
			cin >> quantity;
			book4 *= quantity;
			price4 *= quantity;
			break;

		case 'e':
		case 'E':
			cout << "Quantity for 'Virginia Andrews'?";
			cin >> quantity;
			book5 *= quantity;
			price5 *= quantity;
			break;

		default:
			cout << "The letter you entered didn't match the following criteria";
			cout << "\n Please enter another letter" << endl;
			break;
	}
	}
}

My C++ Code

1>------ Build started: Project: ONLINE, Configuration: Debug Win32 ------
1>  ONLINE.cpp
1>c:\users\sony\documents\visual studio 2010\projects\online\online\online.cpp(47): warning C4700: uninitialized local variable 'book1' used
1>c:\users\sony\documents\visual studio 2010\projects\online\online\online.cpp(55): warning C4700: uninitialized local variable 'book2' used
1>c:\users\sony\documents\visual studio 2010\projects\online\online\online.cpp(63): warning C4700: uninitialized local variable 'book3' used
1>c:\users\sony\documents\visual studio 2010\projects\online\online\online.cpp(71): warning C4700: uninitialized local variable 'book4' used
1>c:\users\sony\documents\visual studio 2010\projects\online\online\online.cpp(79): warning C4700: uninitialized local variable 'book5' used
1>  ONLINE.vcxproj -> c:\users\sony\documents\visual studio 2010\Projects\ONLINE\Debug\ONLINE.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

My error after debugging

It won't obey the case and show default limitlessly. I don't know why.
The warnings are telling you that you need to initialize those variables to some value before you try to use them in calculations.

As for your problem with the switch, your variable choice is an int, you can't enter a character into an int. Maybe you should think about using a char instead of the int. Also if you change to a char you won't be able to enter -1 to exit, a char only holds one character, maybe use another character to signify you want to exit, maybe X or Q?

Following are soem problems that I managed to spot in your code:
1. As your "warnings" prompt you, you haven't initialized the values of book1, book2 etc. and you are using statements like book1 *= quantity;
2. your while loop checks for the value of counter
while (counter != -1) but the value of counter is never changed from 0. (infinite loop)
3. Instead of int, make choice as char type. Also, instead of putting two cases together ('a' and 'A') I would just switch uppercase choice.

Edit: sorry jib didnt see your post. most of my reply is a repitition of what you said :)
Last edited on
You defined variable choice as having type int. So when you try to enter a value for the variable with operator >> the input stops when it encounteres a non-digit. So choice will not be changed (if I am not mistaken).
Define choice as having type char.
Last edited on
So I changed my code a little but it still didn't satisfy the case. It doesn't loop infinitely but when I type any number (including -1), it still only display the default.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	cout << "Welcome to BOOKAZINEWEB!";
	cout << "Books Available in the BOOKAZINE WEB";
	cout << setw(3) << "\n 1 (Ladybird - $15.98)";
	cout << setw(3) << "\n 2 (Sidney Sheldon - $25.50)";
	cout << setw(3) << "\n 3 (Nora Roberts - $28.45)";
	cout << setw(3) << "\n 4 (Penguin Classics - $40.55)";
	cout << setw(3) << "\n 5 (Virginia Andrews - $42.98)";
	
	int counter =0;
	int quantity;
	
	int book1 = 0;
	int book2 = 0;
	int book3 = 0;
	int book4 = 0;
	int book5 = 0;

	double price1 = 15.98;
	double price2 = 25.50;
	double price3 = 28.45;
	double price4 = 40.55;
	double price5 = 42.98;

	while (counter != -1 )
	{

	int choice;

	cout << "\nPlease input a number based on the book type (-1 to checkout)";
	cin >> choice;

	switch (choice)
	{
		case '1':
			cout << "Quantity for'Ladybird'?\n";
			cin >> quantity;
			book1 *= quantity;
			price1 *= quantity;
			break;

		case '2':
			cout << "Quantity for 'Sidney Sheldon'?";
			cin >> quantity;
			book2 *= quantity;
			price2 *= quantity;
			break;
			
		case '3':
			cout << "Quantity for 'Nora Roberts'?";
			cin >> quantity;
			book3 *= quantity;
			price3 *= quantity;
			break;

		case '4':
			cout << "Quantity for 'Penguin Classics'?";
			cin >> quantity;
			book4 *= quantity;
			price4 *= quantity;
			break;

		case '5':
			cout << "Quantity for 'Virginia Andrews'?";
			cin >> quantity;
			book5 *= quantity;
			price5 *= quantity;
			break;

		default:
			cout << "The number you entered didn't match the following criteria";
			cout << "\n Please enter another number" << endl;
			break;
	}
	}
}
1. Switch from 'output' to 'code' (the <> symbol)
2. The problem is: case '1': should be case 1: and so on
Last edited on
i'm surprised you are not getting an infinite loop. where are you changing counter to -1?
Also, if you enter -1, it will go to default because none of the case values are met.
Thank you guys. My program works. :)
Topic archived. No new replies allowed.