Help with creating a loop?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 3736 Views - Last Post: 27 March 2012 - 12:46 PM Rate Topic: -----

#16 TommehTGOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 25-March 12

Re: Help with creating a loop?

Posted 25 March 2012 - 08:50 PM

man...its not working for me...
is there any other way??
Was This Post Helpful? 0
  • +
  • -

#17 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Help with creating a loop?

Posted 25 March 2012 - 09:16 PM

I think what we have here is another HELP VAMPIRE who's just going to keep prompting you to feed code a line at a time until you've written the entire program for him. If we don't see some serious effort on the part of the OP in the next post or two, we should probably close the thread.
Was This Post Helpful? 0
  • +
  • -

#18 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Help with creating a loop?

Posted 25 March 2012 - 09:28 PM

Lol maybe or just struggling.

Another way I was thinking is just one loop and assume ID is always bigger than 100.


Here is some pseudocode

while (!eof)
{
  infile >> value;

  // check if new ID
  if (value > 100)
  {
    // this is an new ID

    // update data

  }
  else
  {
   // this is a grade
   grade = value;
   // get hours as well
   infile >> hours; 

   // update data

  }

}


Was This Post Helpful? 0
  • +
  • -

#19 TommehTGOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 25-March 12

Re: Help with creating a loop?

Posted 25 March 2012 - 09:30 PM

No, don't close it, and no i'm not trying get fed code until i finish it.. I just need that and i'm good to go from there. I'm trying hard to explain what's going on in my code and I don't wanna use such code that we haven't learned in class. That's why i'm asking about this certain part. If I can get lead in the right track, that's all I need for this.
I did what was told, create a inner loop in the main loop, added infile>>ID in the outer loop, then i created the inner loop with is
"while (infile>>ID)
{

}
All i need to know is how to approach to what goes inside the inner loop because i'm not understanding where your going with newline character thing. I'm trying to by step by step when i do this..Cause this is what i want to achieve first..
1.)I want all the ID's to show up first, then i move on to the next stuff, but i'm still stuck in this same part. I'm sorry If i'm annoying or aggravating anyone..
Was This Post Helpful? 0
  • +
  • -

#20 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Help with creating a loop?

Posted 25 March 2012 - 09:51 PM

The two loops and checking for the newline is not working properly for me either. Needs a closer look.

The other idea I had of using one loop should be simpler.

Edit:the read fails somewhere.

This post has been edited by #define: 25 March 2012 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#21 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Help with creating a loop?

Posted 25 March 2012 - 10:14 PM

I had in mind something like this (to control the inner loop)
        while( (test=in.peek()) != '\r' &&  test != '\n' ) {



(test is an int)

This post has been edited by r.stiltskin: 25 March 2012 - 10:14 PM

Was This Post Helpful? 0
  • +
  • -

#22 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Help with creating a loop?

Posted 25 March 2012 - 10:29 PM

If you think that looks too 'advanced', the best C++ way that I can think of is to use getline to read an entire line into a std::string, then put that string into an istringstream, and then just extract the input from the istringstream. Very easy.

If that's also too advanced, TommehTGOD, what method is described in the "chapter on loops" mentioned in your assignment?

And, by the way, do you really want ID to be a double?

This post has been edited by r.stiltskin: 25 March 2012 - 10:32 PM

Was This Post Helpful? 0
  • +
  • -

#23 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Help with creating a loop?

Posted 25 March 2012 - 10:38 PM

I found the problem I was having.

I was using infile >> ch to get the character, forgetting that the >> operator skips whitespace by default, so discards the newline character.
I suggested this earlier which will not work.

      char ch;
      infile >> ch;

      if(ch == '\n')
        break;




So I used the get function which works for me, anyway.

      char ch;
      infile.get(ch);

      if(ch == '\n')
        break;





I suggested having the infile>>ID in the while expression earlier but you only need to read it once.


while (!infile.eof())
{
  infile>>ID;

  while (!infile.eof())
  {
    infile >> grade >> crhrs;

    // I breaked from the loop on a newline
  }
  
}


Was This Post Helpful? 1
  • +
  • -

#24 TommehTGOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 25-March 12

Re: Help with creating a loop?

Posted 25 March 2012 - 10:57 PM

View Postr.stiltskin, on 25 March 2012 - 10:29 PM, said:

If you think that looks too 'advanced', the best C++ way that I can think of is to use getline to read an entire line into a std::string, then put that string into an istringstream, and then just extract the input from the istringstream. Very easy.

If that's also too advanced, TommehTGOD, what method is described in the "chapter on loops" mentioned in your assignment?

And, by the way, do you really want ID to be a double?


in the chapter on loops, it really didn't explain anything, it only went over what a while loop, do-while, and for-loop does, using sentinel values, the break and continue statement, nested loops. (that's it really)


View Post#define, on 25 March 2012 - 10:38 PM, said:

I found the problem I was having.

I was using infile >> ch to get the character, forgetting that the >> operator skips whitespace by default, so discards the newline character.
I suggested this earlier which will not work.

      char ch;
      infile >> ch;

      if(ch == '\n')
        break;




So I used the get function which works for me, anyway.

      char ch;
      infile.get(ch);

      if(ch == '\n')
        break;





I suggested having the infile>>ID in the while expression earlier but you only need to read it once.


while (!infile.eof())
{
  infile>>ID;

  while (!infile.eof())
  {
    infile >> grade >> crhrs;

    // I breaked from the loop on a newline
  }
  
}



and #define, how did you get these to work, i did change that while (infile>>ID) to (!infile.eof()), added the infile>>grade>>crhrs, and also added the get function and all the code you provided for that, I didn't get anything working.
Was This Post Helpful? 0
  • +
  • -

#25 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Help with creating a loop?

Posted 25 March 2012 - 11:25 PM

Did you get any errors? What is your code like now?


Are you on linux/unix?

If so you probably need to test for carriage return (forgotten about that).

if(ch == '\n' || ch == '\r')
  break;


Was This Post Helpful? 0
  • +
  • -

#26 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Help with creating a loop?

Posted 25 March 2012 - 11:41 PM

There are many ways to do this that are preferable to using a sentinel, but if a sentinel is the only way you have been taught then you should probably use that. But don't fill an entire line with sentinels. Just put a single -1 at the end of each line. EVERY line, not just the "not full" lines.

Here's another suggestion -- very simple and straightforward I think:

Use
while( infile >> ID )
for the outer loop. This is preferable to using infile.eof() because infile >> ID will "fail" as soon as there are no more numbers available to read and you'll immediately break out of the loop. And this means also that if you enter the loop, the value has already been stored in ID.

Declare another double "temp" = 0 to use to test for the sentinel.

For the inner loop, in pseudocode:
repeat while temp != -1
    input >> temp
    if temp != -1
        temp >> grade
        input >> hours


Note: that's just pseudocode. You will have to translate that into correct C++ syntax.


View Post#define, on 26 March 2012 - 01:25 AM, said:

Are you on linux/unix?

If so you probably need to test for carriage return (forgotten about that).

Carriage return ('\r') is in Windows and old (pre OS X) Macs. Not Linux.

This post has been edited by r.stiltskin: 25 March 2012 - 11:45 PM

Was This Post Helpful? 0
  • +
  • -

#27 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Help with creating a loop?

Posted 26 March 2012 - 12:23 AM

View Postr.stiltskin, on 26 March 2012 - 08:41 AM, said:

View Post#define, on 26 March 2012 - 01:25 AM, said:

Are you on linux/unix?

If so you probably need to test for carriage return (forgotten about that).

Carriage return ('\r') is in Windows and old (pre OS X) Macs. Not Linux.


Ahh, your peeking for the carriage return has confused me. In Windows opening a file in text mode converts reads of CRLF to a newline. While since I used Unix.
Was This Post Helpful? 0
  • +
  • -

#28 TommehTGOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 25-March 12

Re: Help with creating a loop?

Posted 26 March 2012 - 03:15 AM

No i'm on Windows vista laptop.....but i will definitely test it out r.stiltskin...
question: for my code, will i have to make another variable called temp? and you have input>>hours...will that input just be infile?, or you just put also because you're on another system
Was This Post Helpful? 0
  • +
  • -

#29 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Help with creating a loop?

Posted 26 March 2012 - 03:45 AM

View Post#define, on 26 March 2012 - 02:23 AM, said:

View Postr.stiltskin, on 26 March 2012 - 08:41 AM, said:

View Post#define, on 26 March 2012 - 01:25 AM, said:

Are you on linux/unix?

If so you probably need to test for carriage return (forgotten about that).

Carriage return ('\r') is in Windows and old (pre OS X) Macs. Not Linux.


Ahh, your peeking for the carriage return has confused me. In Windows opening a file in text mode converts reads of CRLF to a newline. While since I used Unix.

Yeah, me too. :) I use Linux most of the time. I guess it's only an issue when you're running Linux and you open a file that was written in Windows. But still, having the test for '\r' in there shouldn't hurt anything. At worst it's irrelevant.

View PostTommehTGOD, on 26 March 2012 - 05:15 AM, said:

No i'm on Windows vista laptop.....but i will definitely test it out r.stiltskin...
question: for my code, will i have to make another variable called temp? and you have input>>hours...will that input just be infile?, or you just put also because you're on another system

No, I said that was pseudocode. I was just using >> to indicate assignment of a value. So "input >> hours" means assign a value from the file to hours, and "temp >> grade" means assign the value from temp to grade.

And I suggested declaring a new variable called "temp" so you can read each value into that and then check to see if it's a sentinel before you assign it to grade.
Was This Post Helpful? 0
  • +
  • -

#30 TommehTGOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 25-March 12

Re: Help with creating a loop?

Posted 26 March 2012 - 04:06 AM

Quote

No, I said that was pseudocode. I was just using >> to indicate assignment of a value. So "input >> hours" means assign a value from the file to hours, and "temp >> grade" means assign the value from temp to grade.

And I suggested declaring a new variable called "temp" so you can read each value into that and then check to see if it's a sentinel before you assign it to grade.


Thanks for the help...i'm sorry again though..I'm trying to use the only resources that we only been taught so far...We haven't learned temp yet, but if you think i should use it, I'll give it a try. but i did put a -1 only once on every line as told. But i'll tell you how far i've gotten on the code before i tried to attempt on what you gave me above

i switched out the while (!infile.eof()) and replaced it with (infile>>ID). Now from the start, all I want to show (right now) in the output is the ID numbers, not the grade and hours yet.
but what's happening is everytime i do that, the ID shows up, and so do the grades and hours as well..
it looks like this

ID GPA
30713
4
3
3
4
3
2
3
etc
etc
19364
etc

if there's anyway I can get to where just the ID show up...i'll be in the great shape..And I'm not smart enough to interpret with the information you're given me, then i'm sorry. I"m a freshman in college still learning
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3