473,404 Members | 2,114 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,404 software developers and data experts.

Loop problem, HELP please!

Hello, I've got a 25 line file with lines of data like this:
sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
The first field is the code that determines what to do. I need to loop
through this data file and, based on that first field, execute different
cases in a switch statement (yes, there's only one now...I'm in a
building process, and I need to get the first one to read first.).
However, it's only going through 1 time and it should be 3 (there are
three lines in the file with sp as the first field). What am I doing
wrong?! Thanks.

int code;
code = readInFirstChars(fin);
while (!fin.fail())
{
switch (code)
{
case 1:
{
readInASpinnerbait(fin, spinList);
}
default:
{
fin.ignore(80, '\n');
}
}
code = readInFirstChars(fin);
}

int readInFirstChars(ifstream &fin)
{
char first;
char second;
int code = 999;
char junk = '/';

first = fin.get();
if (first == 's')
{
second = fin.get();
}
if (second == 'p')
{
code = 1; // 1 is for Spinnerbait
return code;
}
else if (second == junk)
{
fin.ignore(80, '\n');
return code;
}
return code;
}

Jul 22 '05 #1
7 1476
"Francis Bell" <ph**********@charter.net> wrote...
Hello, I've got a 25 line file with lines of data like this:
sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
The first field is the code that determines what to do. I need to loop
through this data file and, based on that first field, execute different
cases in a switch statement (yes, there's only one now...I'm in a
building process, and I need to get the first one to read first.).
However, it's only going through 1 time and it should be 3 (there are
three lines in the file with sp as the first field). What am I doing
wrong?! Thanks.

int code;
code = readInFirstChars(fin);
while (!fin.fail())
{
switch (code)
{
case 1:
{
readInASpinnerbait(fin, spinList);
}
default:
{
fin.ignore(80, '\n');
}
}
code = readInFirstChars(fin);
}

int readInFirstChars(ifstream &fin)
{
char first;
char second;
int code = 999;
char junk = '/';

first = fin.get();
if (first == 's')
{
second = fin.get();
}
So, if the first isn't an 's', it will never attempt to get the 'second'
from the file, and so the following comparison will attempt to compare
the contents of the _uninitialised_ 'second' variable to 'p'. How do
you suppose that's going to work?
if (second == 'p')
{
code = 1; // 1 is for Spinnerbait
return code;
}
else if (second == junk)
{
fin.ignore(80, '\n');
return code;
}
return code;
}


Take a proper line and walk through your code doing what your program
would do, and then proceed with the second line (as you should), and
see what it's going to do. Correct the behaviour as you see fit.

Victor
Jul 22 '05 #2
Victor Bazarov wrote:
"Francis Bell" <ph**********@charter.net> wrote...
Hello, I've got a 25 line file with lines of data like this:
sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
The first field is the code that determines what to do. I need to loop
through this data file and, based on that first field, execute different
cases in a switch statement (yes, there's only one now...I'm in a
building process, and I need to get the first one to read first.).
However, it's only going through 1 time and it should be 3 (there are
three lines in the file with sp as the first field). What am I doing
wrong?! Thanks.

int code;
code = readInFirstChars(fin);
while (!fin.fail())
{
switch (code)
{
case 1:
{
readInASpinnerbait(fin, spinList);
}
default:
{
fin.ignore(80, '\n');
}
}
code = readInFirstChars(fin);
}

int readInFirstChars(ifstream &fin)
{
char first;
char second;
int code = 999;
char junk = '/';

first = fin.get();
if (first == 's')
{
second = fin.get();
}

So, if the first isn't an 's', it will never attempt to get the 'second'
from the file, and so the following comparison will attempt to compare
the contents of the _uninitialised_ 'second' variable to 'p'. How do
you suppose that's going to work?

if (second == 'p')
{
code = 1; // 1 is for Spinnerbait
return code;
}
else if (second == junk)
{
fin.ignore(80, '\n');
return code;
}
return code;
}

Take a proper line and walk through your code doing what your program
would do, and then proceed with the second line (as you should), and
see what it's going to do. Correct the behaviour as you see fit.

Victor

Hello, and thanks. I wish I could say I understand what you are talking
about, but I don't. I've been looking at this and walking through it
for so long trying to figure out what needs to be done that I can't see
straight. And I wish I knew what behavior to correct. But I don't.

Frank

Jul 22 '05 #3
* Francis Bell <ph**********@charter.net> schriebt:

Victor

Hello, and thanks. I wish I could say I understand what you are talking
about, but I don't. I've been looking at this and walking through it
for so long trying to figure out what needs to be done that I can't see
straight. And I wish I knew what behavior to correct. But I don't.


Just do exactly as Victor says.

Start at the first line in your program.

Do what the computer will do with that (not what you wish it to do).

Then the second line, perform it mechanically as the computer will.

And so on.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4
> Hello, and thanks. I wish I could say I understand what you are talking
about, but I don't. I've been looking at this and walking through it
for so long trying to figure out what needs to be done that I can't see
straight. And I wish I knew what behavior to correct. But I don't.


It helps to indent your code properly to see this sort of problem, I've also
added to comment to serve as labels

int readInFirstChars(ifstream &fin)
{
char first;
char second;
int code = 999;
char junk = '/';

first = fin.get(); // A
if (first == 's') // B
{
second = fin.get();
}
if (second == 'p') // C
{
code = 1; // 1 is for Spinnerbait
return code;
}
else if (second == junk)
{
fin.ignore(80, '\n');
return code;
}
return code;
}

Now you are fixated on the sp/ case, but imagine a line that doesn't start
with s, say x instead.

line A, reads 'x', assigns 'x' to first
line B, first == 's' is not true, skip if statement, go to line C
line C, what is the value of second? It has never been given a value, you
have undefined behaviour here.

You need a few more else's in your code to cover all the possibilities.

if (first char is 's')
{
if (second char is 'p')
{
if (third char is '/')
{
}
else
{
}
}
else
{
}
}
else
{
}

But really this is madness, why did you stop using get(first, 4, '/')? get
will work Seems like you are trying to code the behaviour of get by hand.
Even better would be getline, but you were prejudiced against that for some
reason.

Do it like this, no messing

string first;
getline(fin, first, '/');
if (first == "sp")
{
return 1;
}
else
{
fin.ignore(80, '\n');
return 999;
}

Very simple, and you can easily add more cases as you develop your code.

john
Jul 22 '05 #5
The main point here is that you should do it on paper one line at a time.
Assume nothing. Just take each line on its own merit, and do it on papre
exactly. If you do this, the problem will jump out at you.

you'd be amazed how often an elusive bug can be tracked down this way.
Jul 22 '05 #6
Dan Moos wrote:
The main point here is that you should do it on paper one line at a time.
Assume nothing. Just take each line on its own merit, and do it on papre
exactly. If you do this, the problem will jump out at you.

you'd be amazed how often an elusive bug can be tracked down this way.


While it was my suggestion to step through the program one statement
at a time, I can see where it can be difficult for somebody with limited
knowledge of programming. No offense meant toward anyone, but some
novice programmers cannot write code that works or cannot debug their
code, often simply because they are not sure what the constructs they are
to use actually do, or if they are sure (or think that they are sure),
their understanding can easily be incorrect or incomplete.

To the OP:

In order to see what your program is in fact doing, you need to make
sure you know what each statement does and what its side effects are.
Even if you think you know what it does and what comes out of it, put
that "knowledge" aside, take the book, and verify.

The ability to "debug" your programs on a sheet of paper doesn't come
natural to people. It's developed, just like any other skill. I wasted
a lot of scrap paper in the beginning of my career (without symbolic or
visual debuggers at my disposal at the time), but it did teach me to pay
attention.

Try to put aside your disgust with the non-working program, your
frustration with your own inability to see the culprits (yet), and
start anew. You could, of course, just write a new program altogether.
It sometimes helped me, I remember. But it is rarely necessary. What
you wrote is already probably close.

Don't get annoyed because you have to look through it again and again,
that's what programming is about (among other things), just like many
other activities involving creative process and reaching a given goal.
Don't get angry at the newsgroup when people in it do not just point
out all your mistakes and tell you how to correct them. The whole idea
of doing your own work is that in the future you don't have to ask for
help as often because you learn. And believe me, learning by doing is
much more effective than learning by listening.

Good luck!

Victor
Jul 22 '05 #7
Francis Bell wrote:
Hello, I've got a 25 line file with lines of data like this:
sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
The first field is the code that determines what to do. I need to loop
through this data file and, based on that first field, execute different
cases in a switch statement (yes, there's only one now...I'm in a
building process, and I need to get the first one to read first.).
However, it's only going through 1 time and it should be 3 (there are
three lines in the file with sp as the first field). What am I doing
wrong?! Thanks.

int code;
code = readInFirstChars(fin);
while (!fin.fail())
{
switch (code)
{
case 1:
{
readInASpinnerbait(fin, spinList);
}
default:
{
fin.ignore(80, '\n');
}
}
code = readInFirstChars(fin);
}

int readInFirstChars(ifstream &fin)
{
char first;
char second;
int code = 999;
char junk = '/';

first = fin.get();
if (first == 's')
{
second = fin.get();
}
if (second == 'p')
{
code = 1; // 1 is for Spinnerbait
return code;
}
else if (second == junk)
{
fin.ignore(80, '\n');
return code;
}
return code;
}


I agree with John Harrison, in that you should
be using a string and getline().

However, strings cannot be used in a switch statement,
and a ladder of if-elseif statements can get pretty
ugly to read. A better result, IMHO, is to use
a map of <string, function pointer>. You would use
the string to find the appropriate function that
would handle the rest of the input.

Example:
#include <istream>
#include <string>
#include <cstdlib>
#include <map>
using namespace std; // because I'm lazy.

// Define a function pointer type.
typedef void (*PTR_PROCESS_FUNC)(istream&);

// Define a function for handling the "sp" case
void Handle_Sp(istream& inp)
{
string text;
getline(inp, text, '\n');
cout << "Handling case for sp\n";
cout << text;
cout.flush();
return;
}

// Define a map {associated array} type
typedef map<string, PTR_PROCESS_FUNC> Process_Map;

int main(void)
{
Process_Map func_map;

// Initialize the map.
// Associate the string "sp" with the Handle_Sp
// function.
func_map[string("sp")] = Handle_Sp;

string text;
istream inp("Your filename");
while (getline(inp, text, '/'))
{
PTR_PROCESS_FUNC function_ptr;
Process_Map::const_iterator iter;

// Search for the function pointer associated
// with the input string.
iter = func_map.find(text);

// Check if the text was found in the map.
if (iter != func_map.end())
{
function_ptr = iter->second;
function_ptr(inp); // Execute the function.
}
else
{
cerr << "No function associated with "
<< text
<< "\n";
inp.ignore(1000, '\n'); // ignore the rest of the line.
}
} // End: while each line.

return EXIT_SUCCESS;
}

--
Thomas Matthews

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

Jul 22 '05 #8

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

Similar topics

6
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element...
3
by: deko | last post by:
Problem: why doesn't this With block work? is it possible to have a For Each loop within a With block? With objWord .Documents.Add Template:=strTemplate, NewTemplate:=False, DocumentType:=0...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
13
by: israphelr | last post by:
Hi all. I have a problem with some code :( ----------- hint = raw_input("\nAre you stuck? y/n: ") hint = hint.lower() while (hint != 'n') or (hint != 'y'):
5
by: GeekBoy | last post by:
Thanks for the help obnoxious. Reading in the rest of the line with "getline" worked great and now the averages work. Now for the new problem. Program on first run works flawlessly. Next runs...
5
by: justbovo | last post by:
Hello, My name is Justin. I am working on a part of a Find Memory module for a program. Here's how it works.. I enter '13' at the main menu which branches out to my Find Memory module. I ask...
6
by: markus.litz | last post by:
Hello, I have a strange problem with some of my c++ code. I have a normal for- loop like this for(int i = 1; ..... on MS-Windows with a Microsoft compiler everything is alright, but when I...
1
by: jrayjr | last post by:
Hello, I’m running into a problem when I am trying to validate my form. I have a form that has 10 sections I need to validate with 70 inputs that the user must fill out. I tried doing it all in if...
5
by: Poly | last post by:
Here is a block of code that I am having trouble with. I rewrote it as a main function, so I could isolate the problem to fix it, but still haven't been able to figure out the problem. I am pretty...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.