Hard Light Productions Forums

Off-Topic Discussion => General Discussion => Topic started by: Liberator on March 22, 2004, 11:55:40 pm

Title: C++ newbie needs help
Post by: Liberator on March 22, 2004, 11:55:40 pm
Okay this is a program I have to write for my C++ class.  I really don't want any comments as to how poor the coding is or some advanced feature that might do it easier.  We are supposed to write a program that write makes use of a user defined class, and the program does this I just can't get the marked line to work, my instructor couldn't even figure it out and he's been coding C++ for years.  Help!


#include
#include
using namespace std;

class AcctMon
{
private:
   string fname, lname;
   string address;
   string city, state, zip;
   float bal;

public:

   void PrintLabel(void)
   {
      cout << fname << " " << lname << endl;
      cout << address << endl;
      cout << city << ", " << state << " " << zip << endl;
   }

   void TestZIP(void)
   {
      string test;
      cout << "Enter the test value.";
      cin >> test;

      if (zip == test)
         cout << "The customer lives within the given ZIP code." << endl;
      else
         cout << "The customer lives outside the given ZIP code." << endl;
   }

   void CheckBal(void)
   {
      cout << "The customer's current balance is " << bal << endl;
   }

   void InputData (void)
   {
      cout << "Enter the Customer's first name." << endl;
      cin >> fname;
      cout << "Enter the Customer's last name." << endl;
      cin >> lname;
      cout << "Enter the Customer's address." << endl;
getline(cin, address);
      cout << "Enter the city. " << endl;
      cin >> city;
      cout << "Enter the state. " << endl;;
      cin >> state;
      cout << "Enter the Customer's ZIP code. " << endl;
      cin >> zip;
      cout << "Enter the Customer's beginning balance. " << endl;
      cin >> bal;
   }

   void UpdateBal(void)
   {
      float newbal, trans;
      int choice = 1;

      while (choice == 1)
      {
         cout << "Enter the transaction.(negative value for subtraction)" << endl;
         cin >> trans;
         newbal = bal + trans;
         bal = newbal;
         cout << "Customer's new balance = " << bal << endl;
         cout << "Select 1 for more transactions, any other number to exit." << endl;
         cin >> choice;
      }
   }
};


void main()
{
   int menu = 99;
   AcctMon customer;

   
   
   while(menu!=0)
   {
      cout << "Select 1 to enter the customer's information." << endl;
      cout << "Select 2 to confirm customer's zone" << endl;
      cout << "Select 3 to print a mailing label" << endl;
      cout << "Select 4 to update the customer's account balance" << endl;
      cout << "Select 5 yto check view the customer's current balance." << endl;
      cout << "Select 0 to exit" << endl;
     cin >> menu;   
      switch(menu)
      {
      case 1:
         customer.InputData();break;
      case 2:
         customer.TestZIP();break;
      case 3:
         customer.PrintLabel();break;
      case 4:
         customer.UpdateBal(); break;
      case 5:
         customer.CheckBal();break;
      case 0:
         break;
      default:
         cout << "Enter a valid selection.";
      };
   }
}



Currently, the program goes into a infinite loop after the marked line throws, it shouldn't obviously.  My instructor said I can turn it in with that line coded to with no spaces in the particular piece of data, but since it's an address, it really needs to have the spaces.
Title: C++ newbie needs help
Post by: Anaz on March 23, 2004, 12:05:48 am
putting a cin.ignore(80, '\n'); right before your marked line should do the trick.


1 question though...why on earth are you uh..."fleshing out" the functions inside the object definition? Everything I've ever learned about structured programming says to do it outside...
Title: C++ newbie needs help
Post by: StratComm on March 23, 2004, 12:16:59 am
I agree Anaz, it's highly unconventional.  That aside, there is one thing that I do notice:  From a robustness standpoint anyway, city and state should use the same (getline) method, since you have some multi-word states and a lot of multi word cities.  The getline function has always presented problems in the cin environment for me, but what Anaz says most certainly makes sense.
Title: C++ newbie needs help
Post by: Liberator on March 23, 2004, 12:32:04 am
Anaz, what exactly would that do from a processing standpoint?  More correctly, what does that tell the compiler to do?

And as far as the Function construction, it's the way I was taught and my instructor seems to know what he's doing.  He wrote code professionally for 20+ years.
Title: C++ newbie needs help
Post by: Anaz on March 23, 2004, 12:41:47 am
Quote
Originally posted by Liberator
Anaz, what exactly would that do from a processing standpoint?  More correctly, what does that tell the compiler to do?

And as far as the Function construction, it's the way I was taught and my instructor seems to know what he's doing.  He wrote code professionally for 20+ years.


It ends up doing the exact same thing from a processing standpoint, it just makes the code a helluva lot easier to look at.

Also, your instructor may have written code professionally for 20+ years, but that means he started programming 20+ years ago, and I imagine that he is probably not completely up to date with structured programming, considering that was a bit before most OOP languages IIRC.
Title: C++ newbie needs help
Post by: Liberator on March 23, 2004, 02:47:30 am
Okay, thanks! :D
Title: C++ newbie needs help
Post by: Exarch on March 23, 2004, 04:28:04 am
Code: [Select]
getline(cin, address); That just looks wrong to me. Wouldn't  the correct use be:
Code: [Select]
cin.getline(address, 256, '/n');I could well be wrong as I'm not that experienced in c++ myself yet (mainly done java until some months back), but worth a shot at least :)

Another thing, your main function is set to a void return type which will make some compilers throw a fit :nervous: Declaring the functions inside the class is fine imo when they're as short as these, but I'd definitely suggest against it for big ones. Few other minor nitpicks as well but honestly nothing you need to worry about while just learning the language, so I'll leave it at that.
Title: C++ newbie needs help
Post by: Xelion on March 23, 2004, 05:53:03 am
Quote
Originally posted by Exarch
Code: [Select]
getline(cin, address);
That just looks wrong to me. Wouldn't the correct use be:
Code: [Select]
cin.getline(address);
[/B]


Kind of reminds me about the transition of ActionScript from Flash 5 to MX.
Title: C++ newbie needs help
Post by: Exarch on March 23, 2004, 05:54:00 am
I actually made a mistake there that I just fixed - cin.getline needs a max length and a terminating character as arguments as well :D
Title: C++ newbie needs help
Post by: adwight on March 23, 2004, 08:22:54 am
When I do mine like that I always go:

cin.get(whatever, whatever);
cin.ignore(80, '\n');
Title: C++ newbie needs help
Post by: Kazan on March 23, 2004, 09:22:15 am
cin.getline(char *, int, char)
getline(std::string &, istream&)


-------------------------

PS: The only time you should write a function inline in the class header is when you intend it to be INLINE because that's exactly what happens

The way you wrote your code right there is going to make code be duplicated each time a function is called.

AAHHHH *Flogs your professor*
Title: C++ newbie needs help
Post by: Bobboau on March 23, 2004, 09:23:42 am
getline == trouble
at least in my experience, there always seems to be some bug in it (for MSVC anyway)
Title: C++ newbie needs help
Post by: Kazan on March 23, 2004, 09:24:23 am
in MSVC it's called: Microsoft doesn't know how to implement standard functions
Title: C++ newbie needs help
Post by: Bobboau on March 23, 2004, 09:26:00 am
:nod:

why would they when they have all those wonderfull proprietary functions that won't work on anything but a microsoft system.
Title: C++ newbie needs help
Post by: Exarch on March 23, 2004, 09:38:24 am
Dear god, declaring functions inside the class definition makes them inline? Well, learn something new every day, and that's certainly a good thing to know. None of my c++ books have mentioned that :p Will be keeping that in mind for sure.
Title: C++ newbie needs help
Post by: Bobboau on March 23, 2004, 09:48:10 am
realy, that... might explain a few things
Title: C++ newbie needs help
Post by: Liberator on March 23, 2004, 11:23:02 am
Quote
cin.getline(address);


Actually, I tried it like at school yesterday and it refused to work like I wanted it to.
Title: C++ newbie needs help
Post by: mikhael on March 23, 2004, 02:10:07 pm
Yeah, method declaration inside the class declaration is the same as declaring them outside the class declaration with an inline keyword.

As Kaz says, this results in your method's code getting duplicated EVERY SINGLE PLACE the method is called. For anything more than a one liner (like the simplest accessor or maybe a incrementor), this is insanity: you'll end up with ridiculousl bloated executables. Its just not an acceptable trade off, since you're only saving on a vtable lookup.
Title: C++ newbie needs help
Post by: Goober5000 on March 23, 2004, 05:20:35 pm
The problem with getline is that it isn't fully compatible with other uses of cin.  Cin tends to leave behind an extra newline character, which getline chokes on.  Here's one of my examples:
Code: [Select]
apstring GetApstringLine(apstring prompt)
// This function uses getline to get a line of text.  It takes care of
// the incompatibilities between cin and getline by getting rid of the
// extra newline character left behind by any previous cin's.  If this
// is the first function called in a program, this routine works as it
// should; if a cin is encountered before this function, it takes care
// of that as well.
{
apstring input; // input
char dummy; // dummy variable for possible newline

cout << prompt; // prompt for input

if (cin.peek() == '\n') // if cin left behind a '\n' to foul up getline...
{
cin.get(dummy); // ... then get rid of it
}

getline(cin, input); // get the ungarbagified input

return input; // return it
}
Title: C++ newbie needs help
Post by: Liberator on March 23, 2004, 05:31:39 pm
Just so you know, we haven't studied pointers yet.  Or written any truly complex programs.
Title: C++ newbie needs help
Post by: Goober5000 on March 23, 2004, 05:41:03 pm
Well, neither does that.  I edited my post to display the entire function; it's part of a header file I wrote in high school, when we had to use the apstring class, but it's valid for regular strings also.
Title: C++ newbie needs help
Post by: Kazan on March 23, 2004, 05:44:10 pm
lberator.. nobody told you to do anything with pointers
Title: C++ newbie needs help
Post by: Eternal One on March 23, 2004, 06:00:21 pm
Quote
Originally posted by mikhael
Yeah, method declaration inside the class declaration is the same as declaring them outside the class declaration with an inline keyword.

As Kaz says, this results in your method's code getting duplicated EVERY SINGLE PLACE the method is called. For anything more than a one liner (like the simplest accessor or maybe a incrementor), this is insanity: you'll end up with ridiculousl bloated executables. Its just not an acceptable trade off, since you're only saving on a vtable lookup.


Could sound like nitpicking, but to be accurate, function inlinements are just hints to the compiler. It doesn't force any behaviour, any half-decent compiler doesn't even try to inline to any of the functions beside CheckBal() since the other functions surely get bounced back by cost/benefit analysis.

But anyway, it's true that method declaring should be done outside, even if only because it being clearer.
Title: C++ newbie needs help
Post by: mikhael on March 24, 2004, 10:23:00 am
It is nitpicking, yes, but accurate nitpicking.

So far as I know, most compilers will follow an 'inline' keyword unless told not to for one reason or another. I'd have to go hit up the gcc man page (and reinstall VC++ to read its docs) to see for sure, mind you.

Either way, there's way too many benefits for defining methods outside the class description. I shouldn't have used the word 'declaring' before, since method declaration still has to take place within the class definition. Blah blah blah. Semantics.
Title: C++ newbie needs help
Post by: Kazan on March 24, 2004, 11:00:54 am
there are appropriate times to both declair and define a function in the class header though liberator - don't think we're espousing a universal rule

one line accessors, etc are appropriate like

Code: [Select]

class SomeClass
{
      private:
           int SomeData;
           ...

      public:
          SomeClass(...);
          ~SomeClass();

          void SomeOperation(...);

           int GetSomeData()
                { return SomeData; }

}