GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / 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 01-Dec-2007, 11:32
shadowkun shadowkun is offline
New Member
 
Join Date: Dec 2007
Posts: 1
shadowkun is on a distinguished road

first post


hey whats going on, just had a quick question, iam teaching myself c++ and am in kind of a rut, iam makeing a program that compares zipcodes and tells you the shipping for them, the problem is that its only printing the first value ($25), the program checks the first 3 digits (of 5) to see if its a correct value (either 605 or 606). everything compiles but it seems to just do the 605 part
CPP / C++ / C Code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string zipcode = "";
	cout<<"enter 5 digit zip: "<<endl;
cin >> zipcode;
if(zipcode.length()==5)
{
	cout<<"valid"<<endl;
	
	if(zipcode.compare(0, 3, "605"))
	{	
		int x =25;
		cout<<"Shipping will be $"<<x<<endl;
		return 0;
	}
			else (zipcode.compare(0, 3, "606"));
			{
			int y =30;
			cout<<"Shipping will be $"<<y<<endl;
			return 0;
			}
}
else(zipcode.length()!=5);
	cout<<"invalid zipcode"<<endl;
}
  #2  
Old 01-Dec-2007, 12:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: first post


Quote:
Originally Posted by shadowkun
...everything compiles but...

1. The string::compare function returns an int value of zero if the comparands are equal.

2. Logic flow is made somewhat obscure (to me, at least) by inconsistent indentation.

3. If you are going to use returns after the comparisons, then the "else" isn't really necessary. On the other hand, some people prefer not to have "returns" in the middle of a function. (Maybe not very important for a short program, but gets hard to follow for bigger efforts.)

Consider something like the following:
CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int
main ()
{
  string zipcode;               // not necessary to initialize

  cout << "enter 5 digit zip: " << endl;
  cin >> zipcode;

  cout << "You entered " << zipcode << endl;    // Make sure it's OK

  if (zipcode.length () == 5)
  {
      cout << "valid" << endl;

      if (zipcode.compare (0, 3, "605") == 0)
      {                       // zero if it is a 605
          cout << "Found 605" << endl;  // Make sure where it is
          int x = 25;
          cout << "Shipping will be $" << x << endl;
      }
      else if (zipcode.compare (0, 3, "606") == 0)
      {                       // zero if it is a 606
          cout << "Found 606" << endl;  // Make sure where it is
          int y = 30;
          cout << "Shipping will be $" << y << endl;
      }
  }
  else
  { //(zipcode.length() != 5);
      cout << "invalid zipcode" << endl;
  }
  return 0;
}


Regards,

Dave
  #3  
Old 03-Dec-2007, 18:47
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road

Re: first post


Indentation is very important in programming when others read your program or u doing maintenance about ur program.

This is a good practice.
__________________
Linux is the best OS in the world.
 

Recent GIDBlogNARMY 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
Free Hosting From FridayHosting crazyboy168 Free Web Hosting 0 30-Nov-2007 12:02
The questions you should ask yourself before you make a post on your blog Vahid Open Discussion Forum 0 10-Mar-2007 10:24
HTTP post program generates 302 moved error HarmoniousBotch C Programming Language 4 14-Jan-2007 16:13
Promoting my forum Get paid upto $0.05 Per post Talktechno Member Announcements, Advertisements & Offers 0 10-Oct-2006 01:37

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

All times are GMT -6. The time now is 06:40.


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