Thread: Rewrite code in structure way

  1. #1
    Registered User
    Join Date
    May 2009

    Rewrite code in structure way

    Trying to eliminate a goto: from Schaums C++ book

    double sum = 0.0;
      int x = 1;
      repeat: sum += 1.0 / x++;
      if (x <= N)
    	  goto repeat;
      printf("The sum of the first %d reciprocals is %lf\n", N, sum);
    Want the same result in structure way it says is to use flags..
    which I am not sure were to place...use a boolean flag...

    #define TRUE	1
    #define FALSE	0
    Suggestions- where to put it...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Note: What you have there aren't flags (or, at least, not directly). A flag is just a variable that indicates some condition to you (rather than storing a value that you want to use). Often they have values true and false, but people often just use 0 and 1.

    That said, I can think of no earthly reason why you would use flags in this case. You have pretty literally a one line repeating statement, which seems like the perfect time to use a loop.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Well, first of all, if the book is teaching you C++, you should probably have put this in the C++ section.

    Secondly, if this book is teaching goto for this kind of assignment, it's likely this book is an evil plot to destroy your potential to program.

    Thirdly, a do while loop would be incredibly easy to put in this situation. You would barely need to change anything.

  4. #4
    Registered User
    Join Date
    May 2009
    Go it to work- used the loop as sugguested...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM