Idiotic Question

A

A_StClaire_

hey all,

I'm a newb and a moronic one at that. after a day, still can't figure
this thing out. trying to write a simple program that reads a
(C-style) string and then a double til the user types "quit". then it
tells the user what was entered.

first time through the loop is fine. but all times afterward the first
step of reading the string is skipped, and "quit" makes the program
terminate completely.

plz help and thx in advance




#include <iostream>
using namespace std;

class Pair {
static const int max_size = 100;
char c_array[max_size][max_size];
double d_array[max_size];
int i;
int j;
public:
Pair() {
i = 0;
j = 0;
}
void c_read() {
cout << "\nEnter a string. Type 'quit' now to
exit.\n\n";
gets(c_array[i++]);
}
bool c_quit() {
if(!strcmp(c_array[i - 1], "quit")) {
strcpy(c_array[i - 1], "");
return true;
}
return false;
}
void d_read() {
cout << "\nEnter a number.\n\n";
cin >> d_array[j++];
}
void c_write() {
int x = 0;
while(x < i) {
cout << c_array[x++] << endl;
}
}
void d_write() {
int x = 0;
while(x < j) {
cout << d_array[x++] << endl;
}
}
};

int main() {
Pair A;

for( ; ; ) {
A.c_read();
if(A.c_quit()) break;
A.d_read();
}

cout << "\n\nYour entries were:\n\n";

A.c_write();

cout << endl;

A.d_write();

cin.get();
cin.ignore();
return 0;
}
 
A

Alipha

hey all,

I'm a newb and a moronic one at that. after a day, still can't figure
this thing out. trying to write a simple program that reads a
(C-style) string and then a double til the user types "quit". then it
tells the user what was entered.

first time through the loop is fine. but all times afterward the first
step of reading the string is skipped, and "quit" makes the program
terminate completely.

plz help and thx in advance




#include <iostream>
using namespace std;

class Pair {
static const int max_size = 100;
char c_array[max_size][max_size];
double d_array[max_size];
int i;
int j;

the point of giving a variable name is not to make it as short as
possible. str_array, num_array, str_count, and num_count perhaps (str
and num are common abbrs)? also, PairManager or something might make a
more appropriate class name.
public:
Pair() {
i = 0;
j = 0;
}
void c_read() {
cout << "\nEnter a string. Type 'quit' now to
exit.\n\n";
gets(c_array[i++]);

gets is evil. Read the "bugs" portion of the man page. use std::getline
and std::string, or if you insist on c-style strings,
std::cin.getline(...)
}
bool c_quit() {
if(!strcmp(c_array[i - 1], "quit")) {
strcpy(c_array[i - 1], "");
return true;
}
return false;
}
void d_read() {
cout << "\nEnter a number.\n\n";
cin >> d_array[j++];

FAQ. And this is the heart of your problem.
}
void c_write() {
int x = 0;
while(x < i) {
cout << c_array[x++] << endl;
}
}
void d_write() {
int x = 0;
while(x < j) {
cout << d_array[x++] << endl;
}
}
};

int main() {
Pair A;

A, how inventive of a name.
 
L

Larry I Smith

hey all,

I'm a newb and a moronic one at that. after a day, still can't figure
this thing out. trying to write a simple program that reads a
(C-style) string and then a double til the user types "quit". then it
tells the user what was entered.

first time through the loop is fine. but all times afterward the first
step of reading the string is skipped, and "quit" makes the program
terminate completely.

plz help and thx in advance




#include <iostream>

#include said:
using namespace std;

class Pair {
static const int max_size = 100;
char c_array[max_size][max_size];
double d_array[max_size];
int i;
int j;
public:
Pair() {
i = 0;
j = 0;
}
void c_read() {
cout << "\nEnter a string. Type 'quit' now to
exit.\n\n";

// gets(c_array[i++]);

cin.getline(c_array[i++], max_size);
}
bool c_quit() {
if(!strcmp(c_array[i - 1], "quit")) {
strcpy(c_array[i - 1], "");
return true;
}
return false;
}
void d_read() {
cout << "\nEnter a number.\n\n";

// cin >> d_array[j++];

char tmp[max_size];
cin.getline(tmp, max_size);
d_array[j++] = strtod(tmp, NULL);
}
void c_write() {
int x = 0;
while(x < i) {
cout << c_array[x++] << endl;
}
}
void d_write() {
int x = 0;
while(x < j) {
cout << d_array[x++] << endl;
}
}
};

int main() {
Pair A;

for( ; ; ) {
A.c_read();
if(A.c_quit()) break;
A.d_read();
}

cout << "\n\nYour entries were:\n\n";

A.c_write();

cout << endl;

A.d_write();

cout << "Press Enter to exit." << endl;
cin.get(); // cin.ignore();
return 0;
}


See embedded code changes above.

Also, it is risky mixing C IO and C++ IO on the same
stream. You were using gets() and 'cin'; this may, or
may not, cause problems.

Regards,
Larry
 
A

A_StClaire_

Larry I Smith wrote:

snip;
See embedded code changes above.

Also, it is risky mixing C IO and C++ IO on the same
stream. You were using gets() and 'cin'; this may, or
may not, cause problems.

Regards,
Larry


thx for the tips, both of you. really appreciate it and I'll try to
appear less stupid in the future. thx again, Larry.
 
B

benben

hey all,

I'm a newb and a moronic one at that. after a day, still can't figure
this thing out. trying to write a simple program that reads a
(C-style) string and then a double til the user types "quit". then it
tells the user what was entered.

first time through the loop is fine. but all times afterward the first
step of reading the string is skipped, and "quit" makes the program
terminate completely.

plz help and thx in advance

This is a simple task made far too complicated. For example, you are dealing
with low level concepts like C strings and raw arrays, you are designing a
class that doesn't seem to encapsulate its concept well.

Try to start using the standard library tools (vector, pair, string, etc.)
They are your friends (easy to use, well designed, highly portable) so why
resist?

Regards,
Ben
 
A

A_StClaire_

benben said:
This is a simple task made far too complicated. For example, you are dealing
with low level concepts like C strings and raw arrays, you are designing a
class that doesn't seem to encapsulate its concept well.

Try to start using the standard library tools (vector, pair, string, etc.)
They are your friends (easy to use, well designed, highly portable) so why
resist?

Regards,
Ben


I was not resisting. I was trying to do the task without the tools you
mentioned because I don't know how to use them yet. the one book I've
worked through so far is a beginner's book that ended where the STL
begins, so to speak.

right now I'm trying to read Stroustrup's "The C++ Programming
Language" and quite frankly its exercises are kicking the crap out of
me.
 
M

Mike Wahler

right now I'm trying to read Stroustrup's "The C++ Programming
Language" and quite frankly its exercises are kicking the crap out of
me.

Well, certainly keep that book for later, but perhaps this
one would be better for you to start with:
www.acceleratedcpp.com

It has the reader using the standard library and writing useful
code from the start.

-Mike
 
B

Ben Pope

benben wrote:



I was not resisting. I was trying to do the task without the tools you
mentioned because I don't know how to use them yet. the one book I've
worked through so far is a beginner's book that ended where the STL
begins, so to speak.

Oh, start with the STL before dealing with arrays and pointers. It's very easy to go wrong with those things, but once you start to get used to unfamiliar look of templates and
the STL, you'll realise what it all means, and how easy it is to get it right.

Ben
 
A

A_StClaire_

Ben said:
Oh, start with the STL before dealing with arrays and pointers. It's very easy to go wrong with those things, but once you start to get used to unfamiliar look of templates and
the STL, you'll realise what it all means, and how easy it is to get it right.

Ben

thx, Mike and Ben. taking your advice.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top