473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Idiotic Question


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;
}

Aug 7 '05 #1
8 1465

A_*********@hotmail.com wrote:
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.

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;
}


Aug 7 '05 #2
A_*********@hotmail.com wrote:
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 <cstdlib> // for strtod()
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
Aug 7 '05 #3

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.

Aug 7 '05 #4

<A_*********@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...

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
Aug 8 '05 #5

benben wrote:
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.

Aug 9 '05 #6

<A_*********@hotmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...


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
Aug 9 '05 #7
A_*********@xxxxxxx.com wrote:
benben wrote:

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?


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
--
I'm not just a number. To many, I'm known as a String...
Aug 9 '05 #8

Ben Pope wrote:
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
--
I'm not just a number. To many, I'm known as a String...


thx, Mike and Ben. taking your advice.

Aug 9 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
23
by: novice | last post by:
I dint find a proper group to post this, so i'm asking this question. If you think this question is irrelevent then dont answer, but i expect good replies from experts. I wondor, how these guys...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
4
by: theapeman | last post by:
Sorry to bore everyone with this question, which I'm sure is the equivalent of "Please help! What letter comes between C and E in the alphabet?" but seriously, if you really didn't know that, I don't...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.