Author Topic: C++ newbie needs help  (Read 1623 times)

0 Members and 1 Guest are viewing this topic.

Offline Liberator

  • Poe's Law In Action
  • 210
C++ newbie needs help
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.
So as through a glass, and darkly
The age long strife I see
Where I fought in many guises,
Many names, but always me.

There are only 10 types of people in the world , those that understand binary and those that don't.

 

Offline Anaz

  • 210
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...
Arrr. I'm a pirate.

AotD, DatDB, TVWP, LM. Ph34r.

You WILL go to warpstorm...

 

Offline StratComm

  • The POFressor
  • 212
  • Cameron Crazy
    • http://www.geocities.com/cek_83/index.html
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.
who needs a signature? ;)
It's not much of an excuse for a website, but my stuff can be found here

"Holding the last thread on a page comes with an inherent danger, especially when you are edit-happy with your posts.  For you can easily continue editing in points without ever noticing that someone else could have refuted them." ~Me, on my posting behavior

Last edited by StratComm on 08-23-2027 at 08:34 PM

 

Offline Liberator

  • Poe's Law In Action
  • 210
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.
« Last Edit: March 23, 2004, 12:34:52 am by 607 »
So as through a glass, and darkly
The age long strife I see
Where I fought in many guises,
Many names, but always me.

There are only 10 types of people in the world , those that understand binary and those that don't.

 

Offline Anaz

  • 210
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.
Arrr. I'm a pirate.

AotD, DatDB, TVWP, LM. Ph34r.

You WILL go to warpstorm...

 

Offline Liberator

  • Poe's Law In Action
  • 210
Okay, thanks! :D
So as through a glass, and darkly
The age long strife I see
Where I fought in many guises,
Many names, but always me.

There are only 10 types of people in the world , those that understand binary and those that don't.

 

Offline Exarch

  • 27
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.
« Last Edit: March 23, 2004, 05:52:39 am by 1089 »

 

Offline Xelion

  • 28
  • In the Ether
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.

 

Offline Exarch

  • 27
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

 

Offline adwight

  • Neo-Terran
  • 28
  • Go Gators!
When I do mine like that I always go:

cin.get(whatever, whatever);
cin.ignore(80, '\n');
Neo-Terra Victorious

The Lightning Marshall

158th Banshee Squadron

Gay people are rejects who can't get girls. Period. -DragonClaw
Can I have sex with it yet? -KnightTemplar

 

Offline Kazan

  • PCS2 Wizard
  • 212
  • Soul lives in the Mountains
    • http://alliance.sourceforge.net
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*
PCS2 2.0.3 | POF CS2 wiki page | Important PCS2 Threads | PCS2 Mantis

"The Mountains are calling, and I must go" - John Muir

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
getline == trouble
at least in my experience, there always seems to be some bug in it (for MSVC anyway)
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Kazan

  • PCS2 Wizard
  • 212
  • Soul lives in the Mountains
    • http://alliance.sourceforge.net
in MSVC it's called: Microsoft doesn't know how to implement standard functions
PCS2 2.0.3 | POF CS2 wiki page | Important PCS2 Threads | PCS2 Mantis

"The Mountains are calling, and I must go" - John Muir

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
:nod:

why would they when they have all those wonderfull proprietary functions that won't work on anything but a microsoft system.
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Exarch

  • 27
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.

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
realy, that... might explain a few things
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Liberator

  • Poe's Law In Action
  • 210
Quote
cin.getline(address);


Actually, I tried it like at school yesterday and it refused to work like I wanted it to.
So as through a glass, and darkly
The age long strife I see
Where I fought in many guises,
Many names, but always me.

There are only 10 types of people in the world , those that understand binary and those that don't.

 

Offline mikhael

  • Back to skool
  • 211
  • Fnord!
    • http://www.google.com/search?q=404error.com
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.
[I am not really here. This post is entirely a figment of your imagination.]

 

Offline Goober5000

  • HLP Loremaster
  • 214
    • Goober5000 Productions
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
}
« Last Edit: March 23, 2004, 05:39:24 pm by 561 »

 

Offline Liberator

  • Poe's Law In Action
  • 210
Just so you know, we haven't studied pointers yet.  Or written any truly complex programs.
So as through a glass, and darkly
The age long strife I see
Where I fought in many guises,
Many names, but always me.

There are only 10 types of people in the world , those that understand binary and those that don't.