getline problem

M

MC felon

getline isn't working.....
what's wrong with this code?

#include <iostream>
#include <string>
#include <conio2.h> // ignore this.. my old habits

class base
{
int x,y;
std::string str;
public:
base() {std::cout<<"I'm the constructor of class
base\n"<<std::endl;}
void setdata(int p,int h)
{
x=p;
y=h;
}
void draw()
{
gotoxy(x,y);
std::cout<<"$\n"<<std::endl;
}
void clear()
{
clrscr();
}
void tryagain()
{
using std::cout;
using std::cin;
int main();
cout<<"would you like to try again?\n"<<std::endl;
getline(cin,str);
if(str == "yes" || str == "y")
{
clrscr();
main();
}
else { exit(0); }
}
};


int main()
{
int p,h;
base b;
base* bt;
bt = &b;
std::cin>>p;
std::cin>>h;
bt -> clear();
bt -> setdata(p,h);
bt -> draw();
bt -> tryagain();
return 0;
}


when i finish with the drawing, it says, "would you like to try again?"
and then exits (without waiting
for my reply).
HELP!
 
O

Ondra Holub

MC felon napsal:
getline isn't working.....
what's wrong with this code?

#include <iostream>
#include <string>
#include <conio2.h> // ignore this.. my old habits

class base
{
int x,y;
std::string str;
public:
base() {std::cout<<"I'm the constructor of class
base\n"<<std::endl;}
void setdata(int p,int h)
{
x=p;
y=h;
}
void draw()
{
gotoxy(x,y);
std::cout<<"$\n"<<std::endl;
}
void clear()
{
clrscr();
}
void tryagain()
{
using std::cout;
using std::cin;
int main();
cout<<"would you like to try again?\n"<<std::endl;

// You must skip end of line from first input
cin.ignore(std::numeric_limits said:
getline(cin,str);
if(str == "yes" || str == "y")
{
clrscr();
main();
}
else { exit(0); }
}
};


int main()
{
int p,h;
base b;
base* bt;
bt = &b;
std::cin>>p;
std::cin>>h;
bt -> clear();
bt -> setdata(p,h);
bt -> draw();
bt -> tryagain();
return 0;
}


when i finish with the drawing, it says, "would you like to try again?"
and then exits (without waiting
for my reply).
HELP!

BTW: You are calling main() from tryagain method. That means you are
using recursion. You should better return some value from tryagain
method and test it in main.
 
J

Jack Klein

MC felon napsal:

[snip]
BTW: You are calling main() from tryagain method. That means you are
using recursion. You should better return some value from tryagain
method and test it in main.

You can't use main() recursively in C++. The standard explicitly
prohibits calling main() from within a program.
 
R

red floyd

Jack said:
MC felon napsal:
getline isn't working.....
what's wrong with this code?
[snip]

BTW: You are calling main() from tryagain method. That means you are
using recursion. You should better return some value from tryagain
method and test it in main.

You can't use main() recursively in C++. The standard explicitly
prohibits calling main() from within a program.

For MC and Ondra:

Jack is correct. See 3.6.1, specifically 3.6.1/3.
 
M

Micah Cowan

Ondra said:
MC felon napsal:

// You must skip end of line from first input
cin.ignore(std::numeric_limits<size_t>::max());

That should go after the int extractors, ideally, not before each
getline(). Also, you would want to specify the delimiter, as the
default delimiter is EOF.

std::numeric_limits<size_t>::max() is not the right value: you want
std::numeric_limits<std::streamsize>::max(). These are /never/ the same
type (though POSIX's ssize_t would be a good fit), as size_t is
unsigned whereas streamsize is required to be signed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top