8 Replies - 395 Views - Last Post: 11 April 2011 - 08:37 PM Rate Topic: -----

#1 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Output Error

Posted 09 April 2011 - 01:45 PM

I spent about 2 hours writing this simple program, and about another hour trying to problem solve it.

Here is my code:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
class Person
{

  public:
  //gets
  int getage();
  int getweight();
  std::string getname();
  char getgender();
  //sets
  void setage();
  void setweight();
  void setname();
  void setgender();

  private:
  int itsAge;
  int itsWeight;
  char itsGender;
  std::string name;
};
//Get Method
std::string Person::getname()
{
 return name;
}

int Person::getage()
{
 return itsAge;
}

int Person::getweight()
{
 return itsWeight;
}

char Person::getgender()
{
 return itsGender;
}
//Sets Method
void Person::setname()
{
using namespace std;
string sName="";

while(true){
cout<<"type in person name:";
cin>>sName;
cout<<endl;
  cout<<"Are you sure you want to use this name"<<sName<<"?"<<endl;
  cout<<"Y/N:";
  char answer1;
  cin>>answer1;
  cout<<endl;

  if(answer1=='Y'||answer1=='y')
    {
        name=sName;
  break;
    }
  else if(answer1=='N'||answer1=='n')
    {
   continue;
    }
   else
   {
     cout<<"Invalid input\n";

     cout<<"Press any button to continue\n";
   cin.clear();
   cin.ignore(255, '\n');
   cin.get();
    continue;
   }
  }//loop

}

void Person::setweight()
{
 using namespace std;
 string rawInput;
 int Weight=0;
 while(getline(cin,rawInput))
 {
 istringstream s(rawInput);

  if(s>>Weight)
  {
   if(Weight<=0)
   {
   cout<<"Invalid input\n";
   cout<<"Number needs to be 1 pound or larger\n";
   cout<<"Press any button to continue\n";
   cin.clear();
   cin.ignore(255, '\n');
   cin.get();
    continue;

   }
   else if(Weight>=1500)
   {
    cout<<"Invalid input\n";
   cout<<"Number needs to be 1499 pounds or less\n";
   cout<<"Press any button to continue\n";
   cin.clear();
   cin.ignore(255, '\n');
   cin.get();
   }
   else
   {
     itsWeight=Weight;
   break;

   }

  }//charter test
  else{
      cout<<"Invalid Input"<<endl;
      cout<<"Weight needs to be an number\n";
      cout<<"No Charters allowed"<<endl;

  continue;
  }


 }//loop

}
void Person::setage()
{
using namespace std;
int age=0;
string rawInput;

 while ( getline(cin,rawInput) )
  {
 istringstream s(rawInput);
 if(s>>age)
 {
  if(age<=0)
  {
   cout<<"Invalid input. Age needs to be 1 year old\n or older"<<endl;
   cout<<"press any button to continue.";
   cin.clear();
   cin.ignore(255, '\n');
   cin.get();
   continue;
  }
  else if(age>=250)
  {
   cout<<"Invalid Input. Age is way to high."<<endl;
   cout<<"Age needs to be 249 or lower"<<endl;
   cout<<"press any button to continue.";
   cin.clear();
   cin.ignore(255, '\n');
   cin.get();
   continue;
  }
  else
  {
      itsAge=age;
      break;
  }

 }
 else
 {
  cout<<"Invalid input. Chareters are not allowed."<<endl;
  cout<<"Press any button to continue"<<endl;
  cin.clear();
  cin.ignore(255, '\n');
  cin.get();
   continue;
 }


  }//loop

}

void Person::setgender()
{
using namespace std;
char cGender=0;

  while(true)
  {
   cout<<"What's the gender type?\n";
   cout<<"M/F:";
   cin>>cGender;
   cout<<endl;
   if(cGender=='F'||cGender=='f')
   {
    break;
   }
   else if(cGender=='M'||cGender=='m')
   {
       itsGender=cGender;
    break;
   }
     else {
     cout<<"Invalid input. Press any button to try again."<<endl;
     cin.clear();
     cin.ignore(255, '\n');
     cin.get();
      continue;
     }

  }//loop

}


//beginning of functions
int iPause(int x);

int main()
{
using namespace std;
Person Bob;

Bob.setname();
Bob.setage();
Bob.setweight();
Bob.setgender();

cout<<"Name: "<<Bob.getname()<<endl;
cout<<"Age: "<<Bob.getage()<<endl;
cout<<"Weight: "<<Bob.getweight()<<endl;
cout<<"Gender: "<<Bob.getgender()<<endl;


iPause(1);
}//end of main

int iPause(int x)
{
using namespace std;

if(x==1)
 {
 cout<<endl<<"Press any button to exit"<<endl;
 cin.clear();
 cin.ignore(255, '\n');
 cin.get();

 }
else if(x==2)
 {
 cout<<endl<<"Press any button to continue"<<endl;
 cin.clear();
 cin.ignore(255, '\n');
 cin.get();

 }

 else
 {
 cin.clear();
 cin.ignore(255, '\n');
 cin.get();

 }
 return 0;
}




Code complies fine. but maybe it's in my if statement that draws out this output error..
Here is what I mean.
type in person name:bob

Are you sure you want to use this namebob?
Y/N:y

Invalid input. Chareters are not allowed.
Press any button to continue



Invalid input. Chareters are not allowed.
Press any button to continue



Invalid input. Chareters are not allowed.
Press any button to continue



So within the code I'm trying to error proof within each block of code(or area that takes in input). However, this should not show up in the set/get names block, and when you hit Y for yes, it should just accept it, and move on.

so it's skipping...

I want to keep my error proofing with numbers belong to Integer(only) and Characters belong with char types(only)...
What should I do?

Is This A Good Question/Topic? 0
  • +

Replies To: Output Error

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: Output Error

Posted 09 April 2011 - 02:23 PM

When you ask if the entry is correct at about line 59 you are leaving the newline character in the input buffer. The next thing you do is try to get input with getline(). And since there is a newline character in the buffer getline() uses this and assumes you are finished with your input.

To fix this problem you need to extract the problem newline character with either another cin.get() or with cin.ignore().

Jim
Was This Post Helpful? 2
  • +
  • -

#3 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: Output Error

Posted 09 April 2011 - 03:05 PM

View Postjimblumberg, on 09 April 2011 - 02:23 PM, said:

When you ask if the entry is correct at about line 59 you are leaving the newline character in the input buffer. The next thing you do is try to get input with getline(). And since there is a newline character in the buffer getline() uses this and assumes you are finished with your input.

To fix this problem you need to extract the problem newline character with either another cin.get() or with cin.ignore().

Jim


Where do I add the cin.get() or cin.ignore()? I tried on line 59 and didn't change anything.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: Output Error

Posted 09 April 2011 - 03:18 PM

It would have to be after your cin on line 59 so line 60 seems to work. In your setter functions you should tell the user what they are entering, right now after you say that you want to use the name you go right to a getline() without informing the user that they should enter some piece of data. But if you do enter the correct type of data then the program works.


Jim
Was This Post Helpful? 1
  • +
  • -

#5 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: Output Error

Posted 09 April 2011 - 08:20 PM

View Postjimblumberg, on 09 April 2011 - 03:18 PM, said:

It would have to be after your cin on line 59 so line 60 seems to work. In your setter functions you should tell the user what they are entering, right now after you say that you want to use the name you go right to a getline() without informing the user that they should enter some piece of data. But if you do enter the correct type of data then the program works.


Jim


Ok jim I'll give it a shot. You have earn 2 free points WOOT
Was This Post Helpful? 0
  • +
  • -

#6 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: Output Error

Posted 10 April 2011 - 09:04 PM

Look I hate to keep bringing this up but the solution you ofter is not working

cout<<"Are you sure you want to use this name"<<sName<<"?"<<endl;
  cout<<"Y/N:";
  char answer1;
  cin>>answer1;
//at line 60
 cin.ignore();
  cout<<endl;



I tried it with cin.get(); and still the same problem..
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: Output Error

Posted 11 April 2011 - 07:08 PM

It works for me. Maybe if you put some print outs telling the user what to input prior to your input you might be able to see where the program is going wrong.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Output Error

Posted 11 April 2011 - 08:13 PM

Are you sure you're not just getting confused when your program returns from setname() and continues immediately with setage() which sits and waits for an input without prompting for one ? Are you pressing 'enter' twice after "Are you sure you want to use this name"?

And by the way, is there any particular reason why you have "using namespace std" in every function? Why not put it just once at the top of the file, immediately after the includes?
Was This Post Helpful? 0
  • +
  • -

#9 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: Output Error

Posted 11 April 2011 - 08:37 PM

Ok I'll just start over on my code to fix this problem..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1