Problem with cout

This is a discussion on Problem with cout within the C++ Programming forums, part of the General Programming Boards category; Hi, I have an assignment to create a program that asks the user to input an employee's name, the date ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Problem with cout

    Hi, I have an assignment to create a program that asks the user to input an employee's name, the date and the amount of their paycheck and print out a simulated paycheck. I have almost everything figured out, but I keep getting a compile error. Here is my code:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cctype>
    #include <cstring>
    
    using namespace std;
    
    void getInfo();
    void printLetter(char *, char *, char *, char *);
    void printline(char *line, int &startCount);
    
    bool doAgain();
    
    int main()
    	{
    	getInfo();
    	
    	return 0;
    	}
    void getInfo ()
    {
    	char fname[20], lname[20],  date[11], amount[9];
    	double amt;
    	string amtTxt;
    	
    	 do
    	   {
    		cout << "Please enter the employee's first name:\n";
    		cin >> fname;
    		fname[0] = toupper(fname[0]);
    		cout << "Please enter the employee's last name:\n";
    		cin >> lname;
    		lname[0] = toupper(lname[0]);
    		cout << "Please enter the amount of the check:\n";
    		cin >> amount;
    		amt = atof(amount);
    		if (amt < 0 || amt > 10000)
    		{
    			cout << "That is an invalid check amount.  Please try again.\n";
    			cout << "Please enter the amount of the check:\n";
    			cin >> amount;
    		}
    		
    			cout << "Please enter the date (MM/DD/YYYY):\n";
    			cin >> date;
    			cout << "Here is the way the check will print\n" << endl << endl;
    
    		printLetter(fname, lname, date, amount);
    		} 
    	   while (doAgain());
    }
    void printLetter(char *fname, char *lname, char *date, char *amount)
    {
    	int position;
    	char payee[] = "Pay to the Order of: ";
    	char disDate[] = "                                      Date: ";
    	char dispAmt[] = " $";
    	int dp;
    	string pay;
    	string amtTxt;
    	
    	
    
    	pay = amount;
    	position = 0; 
    	printline(disDate, position);
    	cout << right << date << endl << endl;
    
    	
    	position = 0; 
    	printline(payee, position);
    	cout << fname << " " << lname;
    	cout  <<  setw(10) << dispAmt <<  setprecision(5) <<   showpoint << atof(amount) << endl << endl;
    
    	dp = pay.find('.');
    		if (dp == 4)
    		{
    			for (int i = 0; i < dp; i++)
    			{
    				switch (amount[i])
    				{
    					case '1':
    						amtTxt = "One Thousand ";
    						break;
    					case '2':
    						amtTxt = "Two Thousand ";
    						break;
    					case '3':
    						amtTxt = "Three Thousand ";
    						break;
    					case '4':
    						amtTxt = "Four Thousand ";
    						break;
    					case '5':
    						amtTxt = "Five Thousand ";
    						break;
    					case '6':
    						amtTxt = "Six Thousand ";
    						break;
    					case '7':
    						amtTxt = "Seven Thousand ";
    						break;
    					case '8':
    						amtTxt = "Eight Thousand ";
    						break;			
    					case '9':
    						amtTxt = "Nine Thousand ";
    						break;	
    						
    				}
    			}	
    		}
    
    		if (dp == 3)
    		{
    			for (int i = 1; i < dp; i++)
    			{
    				switch (amount[i])
    				{
    					case '1':
    						amtTxt.append("One Hundred ");
    						break;
    					case '2':
    						amtTxt.append("Two Hundred ");
    						break;
    					case '3':
    						amtTxt.append("Three Hundred ");
    						break;
    					case '4':
    						amtTxt.append("Four Hundred ");
    						break;
    					case '5':
    						amtTxt.append("Five Hundred ");
    						break;
    					case '6':
    						amtTxt.append("Six Hundred ");
    						break;
    					case '7':
    						amtTxt.append("Seven Hundred ");
    						break;
    					case '8':
    						amtTxt.append("Eight Hundred ");
    						break;			
    					case '9':
    						amtTxt.append("Nine Hundred ");
    						break;	
    						
    				}
    			}	
    		}
    		if (dp == 2)
    		{
    			for (int i = 2; i < dp; i++)
    			{
    				switch (amount[i])
    				{
    					case '1':
    						amtTxt.append("Ten ");
    						break;
    					case '2':
    						amtTxt.append("Twenty ");
    						break;
    					case '3':
    						amtTxt.append("Thirty ");
    						break;
    					case '4':
    						amtTxt.append("Fourty ");
    						break;
    					case '5':
    						amtTxt.append("Fifty ");
    						break;
    					case '6':
    						amtTxt.append("Sixty ");
    						break;
    					case '7':
    						amtTxt.append("Seventy ");
    						break;
    					case '8':
    						amtTxt.append("Eighty ");
    						break;			
    					case '9':
    						amtTxt.append("Ninety ");
    						break;	
    						
    				}
    			}	
    		}
    				if (dp == 1)
    		{
    			for (int i = 3; i < dp; i++)
    			{
    				switch (amount[i])
    				{
    					case '1':
    						amtTxt.append("One ");
    						break;
    					case '2':
    						amtTxt.append("Two ");
    						break;
    					case '3':
    						amtTxt.append("Three ");
    						break;
    					case '4':
    						amtTxt.append("Four ");
    						break;
    					case '5':
    						amtTxt.append("Five ");
    						break;
    					case '6':
    						amtTxt.append("Six ");
    						break;
    					case '7':
    						amtTxt.append("Seven ");
    						break;
    					case '8':
    						amtTxt.append("Eight ");
    						break;			
    					case '9':
    						amtTxt.append("Nine ");
    						break;	
    						
    				}
    			}	
    		}
    				cout << amtTxt << "and " << pay.substr(dp) << " cents\n";
    }
    
    void printline(char *line, int &startCount)
    {
    	int charCount = 0;
    
    	if (startCount >= 70) 
    	{                     
    		cout << "\n";     
    		startCount = 0;   
    	}
    
    	
    	while (line[charCount] != '\0')
    	{
    		if (startCount >= 60 && line[charCount] == ' ')
    		{
    			cout << "          \n";  
    			charCount++;             
    			startCount = 0;
    		}
    		if (startCount == 0)
    		{
    			cout << "	";  
    			startCount = 10;
    		}
    		cout.put(line[charCount]); 
    		charCount++;               
    		startCount++;             
    	}
    }
    
    
    bool doAgain()
    {
    	char again;
    	do
    	{
    		cout << "Do you want to run the program again (Y/N)? ";
    		cin >> again;
    		again = toupper(again);
    	} while (again != 'Y' && again != 'N');
    	if (again == 'Y')
    	{
    		system("CLS");
    		return true;
    	}
    	else
    	{
    		cout << "Thank you!!!\nHave a nice day!!!" << endl;
    		return false;
    	}
    }
    I get one warning and one error:
    Code:
    checkwriter.cpp(81) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
    
    checkwriter.cpp(231) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
    It looks like the << after cout is causing an error, but in my book you can use a cout statement with a string data type so I am not sure what the problem is. Can anyone help me?

    Thanks

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    What compiler are you using?? It compiles perfectly with Borland.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    If you are using the string class, you have to #include <string>. I don't see anything in your current code that requires <cstring>, which is for C-style string functions like strcpy and strlen, so you can just change that to <string>. Don't worry about the warning for now. To be technically correct dp should be a string::size_type, but it doesn't really matter in this program.

    Is there a reason you aren't using the C++ string class the entire time? There really isn't a need for C style strings here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getline problem
    By ktar89 in forum C++ Programming
    Replies: 3
    Last Post: 06-24-2007, 06:47 AM
  2. Cout redirection problem
    By MrLucky in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2007, 11:11 AM
  3. cout problem with windows API
    By xximranxx in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2007, 12:37 AM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Weird Problem
    By Xeavor in forum C++ Programming
    Replies: 14
    Last Post: 11-20-2004, 10:23 AM

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