GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Jul-2004, 18:03
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,550
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Can't figure out errors


Running the below code through two compilers (CodeWarrior on XP and Anjuta on Debian) I get errors on the last few lines, specifically:
CPP / C++ / C Code:
else if (phone.size()==7)
and
CPP / C++ / C Code:
else cerr<<"Invalid number.";
Anjuta tells me it's a parse error while CodeWarrior tells me it's a syntax error. I'm aware that they're both telling me the same thing, but I can't figure out exactly what's wrong. I tried the different compilers to see if one might offer more of an explanation. If I comment out the lines, then it works fine. I don't see anything grossly wrong with it. Any ideas?

CPP / C++ / C Code:
/*
Purpose:  Given a phone number, extract and display the area 
code (if long distance), the exchange, and local number.
*/
#include <iostream>
#include <string>

using namespace std;

int main()
{
	cout << "Enter a phone number.  If long distance, include the 1+area code.\n"
		<<"Use format of 5551234 for local and 12085551234 for long distance.\n";
	string phone;
	cin >> phone;
	
	//check for non-number characters
	int letters=phone.find_first_not_of("0123456789", 0);
	if (letters==string::npos)
		cerr<<"Ensure only numbers are entered.";
	
	//check for correct number of digits
	if (phone[0]=='1')
		{
			if (phone.size()==11)
			{
				string not_local=phone.substr(0,1);
				string area_code=phone.substr(1,3);
				string exchange=phone.substr(4,3);
				string local=phone.substr(7,4);
				
				//Output
				cout << "(" << area_code << ")-" << exchange << "-" << local << endl;
			}
			else cerr<<"ERROR:  Long distance number incorrect length.";
		}
	else if (phone.size()==7)
		{
			string exchange=phone.substr(0,3);
			string local=phone.substr(3,4);
			
			//Output
			cout << exchange << "-" << local << endl;
		}
		else cerr<<"Local numbers must have 7 digits.";
	else cerr<<"Invalid number.";
	
	return 0;
}
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #2  
Old 15-Jul-2004, 21:06
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi crystalattice. Where is the matching if statement for this else?
CPP / C++ / C Code:
	else cerr<<"Invalid number.";

If it is this one:

CPP / C++ / C Code:
if (letters==string::npos)
    cerr<<"Ensure only numbers are entered.";
You need to place some braces, otherwise, I don't see a matching if statement for that else.
  #3  
Old 16-Jul-2004, 21:39
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,550
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Thanks for the help. I didn't notice it before; it does work now. However, I have developed a new question. If I leave the code as is, apart from adding the necessary braces, I get the message "Ensure only numbers are entered." every time, regardless if I enter only numbers. If I comment that line out, obviously it works great. How do I get the if statement regarding numbers only to work properly w/o screwing up the rest? I tried rearranging the braces but it just gives compile errors. Would it just be easier to get rid of that if statement and use the generic "invalid number"?
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #4  
Old 17-Jul-2004, 07:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by crystalattice
Thanks for the help. I didn't notice it before; it does work now. However, I have developed a new question. If I leave the code as is, apart from adding the necessary braces, I get the message "Ensure only numbers are entered." every time, regardless if I enter only numbers. If I comment that line out, obviously it works great. How do I get the if statement regarding numbers only to work properly w/o screwing up the rest? I tried rearranging the braces but it just gives compile errors. Would it just be easier to get rid of that if statement and use the generic "invalid number"?
Did you enter dashes? Did you enter a RETURN at the end of the buffer? If you output the entire line entered as ints, you'll probably find the problem:
CPP / C++ / C Code:
for (i=0; i<phone.size()-1, i++)
{
    cout << (int) phone.substr(i,1) << "  ";
}
cout << endl;
Look for an offending value outside 48-57
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
there are some errors but where?? small_ticket C Programming Language 6 18-May-2004 13:10
Error C2143: syntax error : missing ';' before 'type' small_ticket C Programming Language 6 15-May-2004 11:59
Can somebody look at this and point out any errors to me soulfly C Programming Language 7 31-Mar-2004 09:45
Rambus 128mb Pc800 memory errors momo Computer Hardware Forum 1 11-Mar-2004 04:22
PHP Compilation errors! Allowee MySQL / PHP Forum 17 21-May-2003 11:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.