Help please!

This is a discussion on Help please! within the C++ Programming forums, part of the General Programming Boards category; Ok I'm very frusterated as this code isnt working right. Code: #include <iostream> #include <iomanip> using namespace std; int main() ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    11

    Exclamation Help please!

    Ok I'm very frusterated as this code isnt working right.

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std; 	
    int main()
    
    {
    	int code, d1, d2, d3, d4, d5, d6, sumtst, rscdaycalc, rndvous;
    	char rscday ;
    	char chrch = 'church';
    	char lrgt = 'largeree';
    	char fnt = 'fountain';
    	
    	cout << "Please enter a code to break: " << endl;
    	cin >> code;
    	
    	d1 = code / 10000;
    	d5 = code % 10;
    	d4 = (code % 100) / 10;
    	d3 = (code % 1000) / 100;
    	d2 = (code % 10000) / 1000;
    	d6 = code / 100000;
    	
    	if (d6 != 0)
    	{
    		cout << "False message: Not a five-digit number." << endl;
    		return 0;
    	}
    	else if (d1 == 0)
    	{
    	
    	cout << "False Message: Not a five-digit number." << endl; 
    	return 0;
    	}
    	
    	else
    	
    	{
    	
    		sumtst = (d1 + d2 + d3 + d4 + d5);
    	 
    	 	if ((sumtst % 2) != 0)
    	 
    	 	{
    	 	
    	 		cout << "False message: Sum is odd." << endl;
    	 		return 0;
    	 	}
    	 	
    	 	else
    	 	{
    	 	
    	 		rscdaycalc = ((d1 * d2) - d3);
    	 		
    	 			if (rscdaycalc == 1)
    	 		
    	 			 	rscday = 'Monday' ;
    	 			
    	 			else if (rscdaycalc == 2)
    	 				
    	 				rscday = 'Tuesday' ;
    	 			
    	 			else if (rscdaycalc == 3)
    	 			
    	 				rscday = 'Wedesday';
    	 			
    	 			else if (rscdaycalc == 4)
    	 			
    	 				rscday = 'Thursday' ;
    	 				
    	 			else if (rscdaycalc == 5)
    	 			
    	 				rscday = 'Friday' ;
    	 			
    	 			else if (rscdaycalc == 6)
    	 			
    	 				rscday = 'Saturday' ;
    	 				
    	 			else if (rscdaycalc == 7)
    	 			
    	 				rscday = 'Sunday' ;
    	 			
    	 			else
    	 			{
    	 				cout << "False message: Invalid rescue day." << endl;
    	 				return 0;
    	 			}
    	 		}
    	 			if ( d4 > d5)
    	 			{
    	 			rndvous = (d5 + 1);
    	 			
    	 			if (rndvous == 2)
    	 				rndvous = fnt ;
    	 					 			
    	 			else if (rndvous == 7)
    	 				
    	 				rndvous = lrgt ;
    	 				
    	 			else if (rndvous == 8)
    	 			
    	 				rndvous = chrch ;
    	 				
    	 			else
    	 			{
    	 				cout << "False message: Invalid rendezvous point." << endl;
    	 			return 0;
    	 			}	
    	 			}
    	 			
    	 		else if (d4 < d5)
    	 		{
    	 				rndvous = (d5 - 1);
    	 				
    	 				if (rndvous == 2)
    	 			
    	 				rndvous = fnt;
    	 			
    	 			else if (rndvous == 7)
    	 				
    	 				rndvous = lrgt ;
    	 				
    	 			else if (rndvous == 8)
    	 			
    	 				rndvous = chrch ;
    	 				
    	 			else
    	 			{
    	 				cout << "False message: Invalid rendezvous point." << endl;
    	 				return 0;
    	 			}
    	 		}
    	 				
    	 		else
    	 			{
    	 				cout << "False message: Invalid rendezvous point." << endl;
    	 				return 0;
    	 			}	
    	 }
    	 
    cout << "Rescue on " << rscday << " at the " << rndvous << endl;
    return 0;
    
    }
    For starters the compiler comes up with a bunch of errors regarding the char variables. It wont let me make lrgt = large tree or even have the full word wednesday here
    Code:
     
    else if (rscdaycalc == 3)
    	 rscday = 'Wedesday';
    also the final print out if everything else works gives me a print out like " rescue on y at 104"

    Any help is much appreciated as I'm so stuck and can't figure it out.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    In C++, string literals are enclosed in double quotes. char literals(i.e. the characters you assign to a char type variable) are enclosed in single quotes, and char types can only store one character. In C++, a string type is used to store words or sentences(i.e. things that are made up of more than one character). Here is an example:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	char myChar = 'a';
    	string myString = "hello world";
    
    	cout<<myChar<<endl;
    	cout<<myString<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 02-04-2006 at 08:48 PM.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    Ok so I changed all my char variables to string types. The compiler comes up with an error talking about implicit conversion. I really have no idea how to fix it. Is there some other way to go about this?

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    I figured out another way to do it and it worked like a charm.

    Thanks for your help!

Popular pages Recent additions subscribe to a feed

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