newbie Q re mismatched data-types

J

J. Campbell

I'm just starting out with C++, but am experienced with QB 4.5
(compilable Basic).

1st Q: the following code works fine if numbers are entered.
However, if text is entered for either variable, the program goes into
an infinite loop, not stopping for user input at each round. What
causes this behavior, and what's the easy way to check user-input to
make sure it conforms to the variable type?

[Code follows]
#include <iostream>
using namespace std;

int main ()
{
float x = !0;
float y = !0;

cout << endl << "Enter 2 values...I will compare them." << endl <<
"Use 2 Zero's to quit" << endl << endl;

while ( x != 0 || y != 0)
{
cout << "input 1st value ";
cin >> x;
cout << "input 2nd value ";
cin >> y;

if (x > y) {cout << x << " is greater than " << y;}
else
{
if (x < y) {cout << x << " is less than " << y;}
else {cout << x << " and " << y << " are equal";}
}
cout << endl << endl;
}

cout << "Double zero...ending program" << endl;
system ("pause");
return 0;
}
[End Code]

Q2: I've purchaced Jesse Liberty's 'Teach yourself C++ in 24 hours',
and have started the tutorial at cplusplus.com. Can you offer me
pointers to good quality references for learning C++?

Q3: What's a good way to learn what functionality is available in the
standard libraries? How does one go about learning the syntax
required for a given library function?

Thanks for the information.
 
P

pw

(fellow new guy)
As for the tech stuff, this group is full brains and willing people - they
help me along.

http://www.cplusplus.com/ref/ or http://www.cplusplus.com/info/
(get started with brief explanations on common libraries)

Also - standard NG links (provided to me by others)
(at least read the C++ FAQ, has other links there too)
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

John Harrison

J. Campbell said:
John Harrison said:
Thinking in C++ by Bruce Eckel (online at www.mindview.net)

Thanks a bunch, John. The Eckel book looks especially helpful.

I've read the intro, and the first 2 chapters. Looking at the
excercizes at the end of Ch2, they are trivial, except for #7...I
don't have enough knowledge to do this after going through the first 2
chapters.

7. Display a file a line at a time, waiting for the user to press the
"Enter" key after each line.

This is what I have...however it doesn't accept a solo "enter
press"...rather it needs a character before it will print the next
line.

Code:
// "ex7_ch2.cpp"
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream in("ex7_ch2.cpp");
string line;
char holdit;

while(getline(in, line))
{
cout << line << endl;
cin >> holdit;
}
return 0;
}
[end code]

any clue how to do this within the framework of only knowing the first
2 chapters of this book?[/QUOTE]

Simplest way would seem to be this

ifstream in("ex7_ch2.cpp");
string line;
string holdit;

while(getline(in, line))
{
cout << line << endl;
getline(cin, holdit);    // wait for a newline
}

However there a fairly notorius bug in VC++ 5/6 which means this solution
won't work. The bug requires you to type an extra character after the
newline when reading from cin, I think that might be what you are describing
in your post. If you have VC++ 5/6 then make sure you have the latest
service pack.

Another way would be (using the trick I posted earlier)

ifstream in("ex7_ch2.cpp");
string line;

while(getline(in, line))
{
cout << line << endl;
cin.ignore(INT_MAX, '\n');    // wait for a newline
}

All code untested.

john
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top