Illegal Case

This is a discussion on Illegal Case within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble figuring out why my code is giving me an error. Can anyone help? Code: #include<iostream> #include<fstream> #include<iomanip> ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Illegal Case

    I'm having trouble figuring out why my code is giving me an error. Can anyone help?


    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<string>
    
    
    using namespace std;
    
    
    int main ()
    {
    	string name, month, day, yr, loanAmount;
    	string currentDateMonths, currentDateDays,currentDateYears;
    	string totaldays;
    	ifstream infile; 
    	infile.open("data3.txt", ifstream::in);
    	infile >> currentDateMonths >> currentDateDays >> currentDateYears;
    	while ( ! infile.eof ())
    	{
    		infile >> name >> month >> day >> yr >> loanAmount;
    		cout << name << month << day << yr << loanAmount; 
    	}
    	
    	
    	
    	
    
    	infile.close();
    	
    	
    	
    	return 0;
    }
    
    
    //Loan Length
    
    int loanTime ()
    {
    	int month, day;
    	int currentDateMonths, currentDateDays, totaldays;
    	if (month == currentDateMonths)
    		totaldays = (currentDateDays - day);
    	else if (month == (currentDateDays - day))
    		case (month == 1 || 3 || 5 || 7 || 8 || 10 || 12):
    			totaldays = (31 - day) + currentDateDays;
    		case totaldays = (28 - day) + currentDateDays:
    	else if (month == currentDateMonths - 2)
    		case (month == 1 || 2):
    			totaldays = (59 - day) + currentDateDays;
    	else if (month > currentDateMonths)
    		case (month == 11):
    			totaldays = (61 - day) + currentDateDays;
    		case (month == 12):
    			if (currentDateMonths == 1)
    				totaldays = (31 - day) + currentDateDays;
    			else if (currentDateMonths == 2)
    				totaldays = (62 - day) + currentDateDays;
    	else if (month < currentDateMonths)
    		case (month == 11 && currentDateMonths == 1):
    			totaldays = (61 - day) + currentDateDays;
    		case (month == 11 && currentDateMonths >= 2):
    			totaldays = 60;
    		case (month == 12 && currentDateMonths == 1):
    			totaldays = (31 - day) + currentDateDays;
    		case (month == 12 && currentDateMonths == 2):
    			totaldays = (62 - day) + currentDateDays;
    		case (month == 12 && currentDateMonths > 2): 
    			totaldays = (60 - day) + currentDateDays;
    	return totaldays;
    }


    1>------ Build started: Project: Project 3, Configuration: Debug Win32 ------
    1>Compiling...
    1>Project 3.cpp
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(45) : error C2046: illegal case
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(47) : error C2046: illegal case
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(48) : error C2181: illegal else without matching if
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(49) : error C2046: illegal case
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(52) : error C2046: illegal case
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(54) : error C2046: illegal case
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(60) : error C2046: illegal case
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(62) : error C2046: illegal case
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(64) : error C2046: illegal case
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(66) : error C2046: illegal case
    1>c:\users\pat\documents\visual studio 2008\projects\project 3\project 3\project 3.cpp(68) : error C2046: illegal case
    1>Build log was saved at "file://c:\Users\Pat\Documents\Visual Studio 2008\Projects\Project 3\Project 3\Debug\BuildLog.htm"
    1>Project 3 - 11 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




    If you can help me, I'd greatly appreciate it. Thanks!

    -yusko63

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You've got a whole pile of illegal cases in your code. (case is only legal inside a switch.)

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And even then a case label must be a compile-time integral constant.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    While we're at it, this isn't Python. We like indentation, and we want to support your indentation efforts (you've done it really well, here, no joke), but you need to use curly braces and the like to actually create the blocks that your indentation is to represent.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    using eof to control loop is bad idea. read FAQ
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 11:35 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  3. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 10:40 PM
  4. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 11:34 AM
  5. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21