5 Replies - 5267 Views - Last Post: 06 October 2010 - 09:42 AM Rate Topic: -----

#1 Guest_RT100*


Reputation:

Switch - case "xxx was not declared in this scope"

Posted 05 October 2010 - 05:31 PM

hello all.

I am writing a program in C++ that calculates the cost of a long distance call, and I need to cin the day of the week. here's what I have so far: (I am compiling / writing in XCode, btw, if that makes a difference)
# include <iostream>     
using namespace std;

int main ()              // start program
{
	char day_of_week; // variable the stores the day of the week
	double rate_per_min; // variable for cost per minute
	
	cout << "Enter the day of the week as one of the following values:\n" 
	<< "Mo, Tu, We, Th, Fr, Sa, or Su"; 
	// tells the human how to phrase the day of the week
	
	cin >> day_of_week;
	
	switch (day_of_week) 
	{
		case Su:
	
			
		rate_per_min = 0.15;
		break; 
			
			
	        case: Mo // I also include the other days of the week, but
                         // I'm simplifying here
			cout << "Please enter the time of day in 24 hours,"
			<< "for example, 5PM would be entered at 17:00.";
 	                cin >> time
			break;

		default:
			cout << "Error. Day of the Week was entered incorrectly. Please try again.";
			break;
	}
	
	return 0;            // Shows the program ends successfully
}





i have a few questions.

first off, the compiler gives an error on the
case: Su; 

it returns an error
'Su' was not declared in this scope

what does this mean?

second off, can int or double handle time with a colon separating minutes and hours? or do I have to define time as char?

thank you!

Is This A Good Question/Topic? 0

Replies To: Switch - case "xxx was not declared in this scope"

#2 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Switch - case "xxx was not declared in this scope"

Posted 05 October 2010 - 05:40 PM

You can't use strings with a switch. Just a small thing with C++.

and your Su would have to be "Su" anyway.

Secondly you can only store one character in a "char" data type.

So you could store an 'S' in it, or you could maybe use a std::string ?

#include <iostream>
#include <string>

int main()
{
  std::string input;
  std::cout << "S M T W R F" << std::endl;
  std::cout << "Your choice: ";
  std::cin >> input;
  std::cout << "The length of the string: " << input << " is " << input.size() << std::endl;
}


From this example you can see how a string COULD be used.

Quote

second off, can int or double handle time with a colon separating minutes and hours? or do I have to define time as char?


No. They cannot. You cannot use a "char" either. You will either have to use a std::string or an array of characters to hold the data. The array of characters way is a "C string" or a "C style string" and is more difficult to work with than the std::string.

Or you could perhaps store the two pieces separated by the colon separately.



--------
If your assignment is to use a switch statement to do this, you will be accepting input into a "char" variable, which will store only one character.

You can then use your switch with a single character as you were doing. But it would most likely be like this:

char c = 0;
std::cin >> c;
switch(c)
{
  case 'a':
  break;//etc.
//etc



Please also note the single quotes are used for a single character. ('a')

This post has been edited by taylorc8: 05 October 2010 - 05:46 PM

Was This Post Helpful? 1
  • +
  • -

#3 Guest_RT100*


Reputation:

Re: Switch - case "xxx was not declared in this scope"

Posted 05 October 2010 - 05:47 PM

thank you very much for the quick response!

Ah but the assignment calls for me to "store the day of the week in two variables of type char:
Mo Tu We Th Fr Sa Su"

i need to allow the user to do uppercase or lowercase.

I guess I'm still a little confused on how to enter a char variable.

Looking in my textbook, I see that chars can only be a single character (d'oh!) so to combine two chars together, do i need a string?
Was This Post Helpful? 0

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Switch - case "xxx was not declared in this scope"

Posted 05 October 2010 - 05:52 PM

a std::string is a class, a C style string is an array of characters with a NULL character at the end to denote the end of the string.

If you don't know what an array is, this will require reading in your book.

You can create an array of three characters to store the input in, which is a C string. The extra character is needed for the '\0' or the NULL character at the end.

You could get the input with cin.getline() to your array of characters.

Your requirements have dictated that you use two char variables to store the input in.

This is how you do that:

int main()
{
	char c = 0;
	char d = 0;

	std::cin >> c >> d;

	std::cout << c << d;
}



I think I've given you enough information to help you solve your problem at this point.

One problem I forsee is determining which 'T' is which, and that is what you will use the second "char" variable for. The problem will arise because you can't use a switch with strings. Only one character can be a "case".

So I hope you will attempt to complete your assignment, and if you have trouble post here again.


----------
As for the upper or lower case your programming class should have either covered switch statements very well, or the <cctype> header functions.

These functions can be used:
toupper() or tolower() and maybe isalpha()

I should probably point out that with any programming assignment there will usually be more than one way to do it. It is not necessary that you use those functions, you can simply use the behavior of the switch "falling through" from cases to accomplish the task.

Above all it is most important to remember that programming is more of a skill, and I hope that eventually you will become quite a skilled problem solver yourself, with the required knowledge to successfully use C++.

I think you've been given a good explanation of the required knowledge, and perhaps a bit of the problems solved for you, but you still need to implement (code) it.

Any more questions?

This post has been edited by taylorc8: 05 October 2010 - 06:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 RT100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-October 10

Re: Switch - case "xxx was not declared in this scope"

Posted 05 October 2010 - 08:36 PM

I was in the midst of asking a question while converting values from char to int, but figured it out! (subtract 48!)

anyway, my only real problem now is this:
			cout << "Please enter the time that the call was started, from 00:00-23:59." <<  
					"\nPlease include any leading 0 values.\n"; //instructs how to enter in the time format
			cin >> time1 >> time2 >> time3 >> time4 >> time5; //inputs the time chars
			



I have to include that "please include any leading 0 values, because if I just enter in 1:00, it freezes and doesn't continue.

I've tried
            if (time2 == ':') 
            {
                time5 = time4
                time4 = time3
                time3 = ':'
                time2 = time1
                time1 = 0
            }



to try and shift everything one spot to the right, but it just froze and didn't do anything differently.

do you have any ideas on how to solve this?

//edited to subscribe to emails

This post has been edited by RT100: 05 October 2010 - 08:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Switch - case "xxx was not declared in this scope"

Posted 06 October 2010 - 09:42 AM

Quote

do you have any ideas on how to solve this?

Yes, accept your input as a string and take what you need from it.

The best option is probably to ask for an integer, a character, and an integer on the same line for your purpose.

this is what i mean:
int main()
{
	char c = 0;
	int hour,minute;
	hour = minute = 0;

	std::cin >> hour >> c >> minute;
	std::cout << std::endl << hour << c << minute << std::endl;
}



Then you don't have to convert "char" to "int" because you've already got the input as an integer.

This post has been edited by taylorc8: 06 October 2010 - 09:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1