Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
You might've noticed I didn't post a complete solution to his stated problems. It was, as balamw surmised, meant to give an idea how initial states might be handled. Nothing more!

The year before last Lee chastised me for giving complete solutions so I try not to without reason.
 
You might've noticed I didn't post a complete solution to his stated problems. It was, as balamw surmised, meant to give an idea how initial states might be handled. Nothing more!

The year before last Lee chastised me for giving complete solutions so I try not to without reason.

Sorry for chastising. It didn't do some of the handling, but it did a lot of the doing that cybrscot was wrangling with. It looks like he worked it out away from the computer (where problems tend to resolve themselves) anyway, so no harm done.

-Lee
 
Giving a man a fish is robbing him of a learning opportunity. Sometimes it's necessary if someone is really frustrated, but I think progress was being made.

-Lee

Perhaps - but a fish may be either food or fertilizer. And in this case is was offered as fertilizer. Of course if he chose to simply view it as food ...
 
Still no luck, won't compile. I don't know why I'm getting the error that "current_maximum" hasn't been declared, it's declared as a "float" variable.
I'm also not real sure where to place each part of the program, such as after the loop, do I then compare the user number to the maximum number, or do I place that text somewhere else? I also still don't understand where my loop ends.

Lee said, "It looks like the if is the first statement after the "while(...);", so it is not part of the loop. Your loop will end when user_number is less than or equal to 0, so that's whenever a user enters a value that's 0 or negative."

I don't understand when he says that my "if stmt" is the first stmt after the loop. I thought it was part of the loop? Based on do/statement/while/expression/statement mustn't I have a statement after the while/expression to complete writing the loop correctly?

Also, I do understand that my loop ends when a user enters a 0 or a negative number, but don't I need a jump somewhere in there? A break, or continue to actually "get out" of the loop?

Am I getting closer? Let's just keep this as simple as possible for now, for my sanity's sake.



Code:
#include <stdio.h>

main ()

{

  float user_number, current_maximum = 0 ; 
                          
  
  do {
  	printf ("Enter a number: ") ;
  	scanf ("%f", &user_number) ;
  	} while (user_number > 0 ) ;
  		if (user_number > current_maximum)
  			
  	currrent_maximum = user_number ;
  	
        if (user_number <= 0 ) 
  	        printf ("The largest number you printed was: ", current_maximum) ;
  		
  		return 0 ;
  		
}


Terminal Output:

Code:
Scott-Deans-MacBook-Pro:~ scottdean$ gcc ~/documents/do_loop_ch6.c
/Users/scottdean/documents/do_loop_ch6.c: In function ‘main’:
/Users/scottdean/documents/do_loop_ch6.c:16: error: ‘currrent_maximum’ undeclared (first use in this function)
/Users/scottdean/documents/do_loop_ch6.c:16: error: (Each undeclared identifier is reported only once
/Users/scottdean/documents/do_loop_ch6.c:16: error: for each function it appears in.)
 
Perhaps - but a fish may be either food or fertilizer. And in this case is was offered as fertilizer. Of course if he chose to simply view it as food ...

Hey Lloyd, if you offered me the code, I didn't even see it yet. Still stubbornly trying to figure it out on my own.

My paradox is this: I want to be able to do this on my own, and learn as much as possible and have it all sink in, so I can "really" do it and better understand and more quickly comprehend Obj-C when I get there, while at that same time I'm extremely frustrated because I want to learn this more quickly and I want to complete this book and move on. I don't want to be stuck in C still a year from now. I guess I want to get optimum learning with minimum time, to things that are pretty much inversely related.
 
If you look closely you'll see that you have two variables, one with two 'r' in it and another with three 'r'. ;)
 
I'm always feeling a little hesitant to jump back into my code the next day. After wrangling with it for hours without getting it right the night before, it takes some effort to get my mind back in the right frame to do this.

However once I dive in and start working on it again, I get fully immersed and it's difficult to break away as my mind can't stray from thinking about how to do the task at hand.

Once I get started I don't want to stop, once I stop I find it difficult to get started. But I push myself to start because I have a goal.
 
Well, I think we're almost working! Getting excited!!

Code:

Code:
#include <stdio.h>

main ()

{

  float user_number, current_maximum = 0 ; 
                          
  
  do {
  	printf ("Enter a number: ") ;
  	scanf ("%f", &user_number) ;
  	} while (user_number > 0 ) ;
  		if (user_number > current_maximum)
  			
  	current_maximum = user_number ;
  	
        if (user_number <= 0 ) 
  	        printf ("The largest number you printed was: ", current_maximum) ;
  		
  		return 0 ;
  		
}










Terminal Output:

Code:
Scott-Deans-MacBook-Pro:~ scottdean$ gcc ~/documents/do_loop_ch6.c
Scott-Deans-MacBook-Pro:~ scottdean$ ./a.out
Enter a number: 3
Enter a number: 5
Enter a number: 67
Enter a number: 199
Enter a number: 5999
Enter a number: .0556
Enter a number: 0
The largest number you printed was: Scott-Deans-MacBook-Pro:~ scottdean$
 
You're still awake?

I hope so. :D

First, you need a format specifier to print the variable. I added a new line as well.

Code:
printf ("The largest number you printed was: [COLOR="SeaGreen"]%lf\n[/COLOR]", current_maximum)
 
Code:
if (user_number > current_maximum)  			
  	current_maximum = user_number ;
Needs to be in the loop (before the while) or it will never work. In order to get out of the loop user_number is < 0 so user_number > current_maximum is guaranteed to be false and the code will always return 0 as current_maximum even after fixing the printf.

FWIW I noticed something with my version of the code. If I entered the wrong thing like "fred" or "." instead of a number the loop went out of control. I fixed that by initializing user_number to 0 in the loop.

B
 
Lee said, "It looks like the if is the first statement after the "while(...);", so it is not part of the loop. Your loop will end when user_number is less than or equal to 0, so that's whenever a user enters a value that's 0 or negative."

I don't understand when he says that my "if stmt" is the first stmt after the loop. I thought it was part of the loop? Based on do/statement/while/expression/statement mustn't I have a statement after the while/expression to complete writing the loop correctly?
You might want to check out where the book lays out each of the types of loops. In the case of a plain while loop, you have something like:
Code:
while(stmtA) stmtB

where do...while is:
Code:
do 
  stmtA
while(stmtB);

So the "body" of a do...while is stmtA, generally a {}d set of many statements rather than a single statement. With the plain while, the body of the loop comes after the while, rather than between the do and the while in the second case. So "do/statement/while/expression/statement" is not correct, there is no statement after the while that is part of the loop.

Also, I do understand that my loop ends when a user enters a 0 or a negative number, but don't I need a jump somewhere in there? A break, or continue to actually "get out" of the loop?
The loop exits when the condition in the while is falsified. Every time you get to "the end" of the loop, that condition is tested. If it is true, the loop body is executed again. If it is false, the loop exits. No need for a break at all.

balamw already mentioned that your test against current_maximum needs to be pulled into the body of the do...while, for the reason I mentioned above (it is not currently part of the loop). Once you do that you should have it going. There are additional error handling steps you should take, like the initialization balamw, but the algorithm should be working as long as the user enters only numbers.

-Lee
 
I hope so. :D

First, you need a format specifier to print the variable. I added a new line as well.

Code:
printf ("The largest number you printed was: [COLOR="SeaGreen"]%lf\n[/COLOR]", current_maximum)

Thanks Sub!
Another stupid mistake I shoulda' caught.

Ya' know, I'm not yet at the point when I have an error, that I have a system of what to go back and check specific things. Like the 3 r's mistake. I just quickly looked at my float variable, and another place it was in the code, ignoring the assignment operator. I hope that I will soon get to the point that when I see an error in the terminal output, I have the light bulb go off in my head that tells me where it probably is. It was careless of me not to find both of those last two mistakes, I shouldn't have posted those. But I guess when I quickly looked at my code, it looked correct at first glance. I could see that it was declared, unlike what the compiler said.

But as I mentioned above, after I see that it is declared, then the next layer that my mind should peel back is to think to look more closely at spelling. The little things that make all the difference. This is an oversight on my part, but again don't yet have the experience that tells me , "okay, look at this", then, "that's okay, look at that", etc. etc.

I'll get there soon. I push myself pretty hard and mistakes are unacceptable to me. I'm usually very thorough and methodical about the way I go about things. But it helps to have a great deal of familiarity and expertise when trying to be very methodical and disciplined. I'm still trying to see in the dark here!

Don't give up on my gang! I'll get there, I appreciate all the support, you're all far too kind and helpful!
 
OMG, I must be an idiot. My code was compiling but the printf results were always 0's, then I moved the stmt that balamw told me to move inside the loop before the while, and now it won't compile at all. Terminal says I need an "expected expressoin before the } token. But there is an expression before that token, isn't there?

Anyway, I was getting close, I think I'm closer now even though it won't compile because something is out of place. But I don't know what. I seem to have a lot of stuff in my loop. (a lot of statements) but they're all within {} so they are compound stmts I believe.

I also think my code looks like crap. I know the alignments are not right, but not sure how to handle aligning so many different statements and expressions as the code becomes more involved. I also don't know if I could or should have some blank lines/spacing in there somewhere for readabiliy. It looks all too bunched up.

So this is the point that I'd like to ask somebody to throw me the whole fish. If anybody has time, could you rewrite this small program, with proper alignment and spacing so I can examine what it should look like and what should go where?

If I may ask ( I know beggars can't be choosers ), let's just keep it simple to the code at hand and what I'm trying to accomplish for this exercise. Please no code that I don't yet know or it will only confuse me and won't help me to understand how to make "this code" work as it is in this simple form. The simpler the better so I can see how each thing interacts with each other and I can "read" through the code and understand what's happening. As well as where the things I've already done should be placed and used.
Thanks!

Code:
#include <stdio.h>

main ()

{

  float user_number = 0, current_maximum = 0 ; 
                          
  
  do {
  	printf ("Enter a number: ") ;
  	scanf ("%f", &user_number) ;
  	if (user_number > current_maximum)
  	        current_maximum = user_number ;
  	if (user_number <= 0 ) 
  } while (user_number > 0 ) ;
  		
  	 
  	  printf ("The largest number you printed was: %f", current_maximum) ;
  		
  		return 0 ;
  		
}


Code:
Scott-Deans-MacBook-Pro:~ scottdean$ gcc ~/documents/do_loop_ch6.c
/Users/scottdean/documents/do_loop_ch6.c: In function ‘main’:
/Users/scottdean/documents/do_loop_ch6.c:16: error: expected expression before ‘}’ token
Scott-Deans-MacBook-Pro:~ scottdean$
 
Code:
#include <stdio.h>

main ()

{

  float user_number = 0, current_maximum = 0 ; 
                          
  
  do {
  	printf ("Enter a number: ") ;
  	scanf ("%f", &user_number) ;

  	if (user_number > current_maximum)
  	        current_maximum = user_number ;

  	[COLOR="Red"]//if (user_number <= 0 )[/COLOR] 
  } while (user_number > 0 ) ;
  		
  	 
  printf ("The largest number you printed was: %f", current_maximum) ;
  		
  return 0 ;
  		
}


Try this. I commented out the offending line. You didn't have a semicolon at the end of the statement.

EDIT: I think you need to review how while and do-while loops work. A while loop is a pretest loop which means it tests for some condition prior to running. If that condition evaluates to true, the loop runs and then checks the condition again prior to running again. If the condition evaluates to false, the loop does not run and the program continues to the next line after the loop.

while (condition)
{
//statements
}

A do-while loop is a posttest loop which means it tests for some condition after the loop has run at least once. If that condition evaluates to true, the loop runs again and then checks the condition. If the condition evaluates to false, the loop does not run and the program continues to the next line after the loop.

do
{
//statements
} while (condition)

EDIT #2: Instead of commenting out the offending line, what if I had put a semicolon at the end of that statement to make it correct? What impact would that if statement have on the program?
 
Last edited:
You might want to check out where the book lays out each of the types of loops. In the case of a plain while loop, you have something like:
Code:
while(stmtA) stmtB

where do...while is:
Code:
do 
  stmtA
while(stmtB);

So the "body" of a do...while is stmtA, generally a {}d set of many statements rather than a single statement. With the plain while, the body of the loop comes after the while, rather than between the do and the while in the second case. So "do/statement/while/expression/statement" is not correct, there is no statement after the while that is part of the loop.


-Lee


You're right, my bad... I think I made a mistake when making my flashcards and I memorized it the wrong way. Book is correct, flashcards wrong.
 
Code:
#include <stdio.h>

main ()

{

  float user_number = 0, current_maximum = 0 ; 
                          
  
  do {
  	printf ("Enter a number: ") ;
  	scanf ("%f", &user_number) ;

  	if (user_number > current_maximum)
  	        current_maximum = user_number ;

  	[COLOR="Red"]//if (user_number <= 0 )[/COLOR] 
  } while (user_number > 0 ) ;
  		
  	 
  printf ("The largest number you printed was: %f", current_maximum) ;
  		
  return 0 ;
  		
}


Try this. I commented out the offending line. You didn't have a semicolon at the end of the statement.

EDIT: I think you need to review how while and do-while loops work. A while loop is a pretest loop which means it tests for some condition prior to running. If that condition evaluates to true, the loop runs and then checks the condition again prior to running again. If the condition evaluates to false, the loop does not run and the program continues to the next line after the loop.

while (condition)
{
//statements
}

A do-while loop is a posttest loop which means it tests for some condition after the loop has run at least once. If that condition evaluates to true, the loop runs again and then checks the condition. If the condition evaluates to false, the loop does not run and the program continues to the next line after the loop.

do
{
//statements
} while (condition)

Thank you very much for the fish (as fertilizer of course!! )
So I see that everything inside the do is aligned about a tab stop in, the exception is the while.

I'm still getting extra 0's in my output. Do I need to specify my float variable to how many decimal places? But I want the user to be able to enter as large a number as they like, then reprint the largest number, but without any extra 0's as baggage.


Code:
Scott-Deans-MacBook-Pro:~ scottdean$ ./a.out
Enter a number: 9898
Enter a number: 74
Enter a number: 39
Enter a number: .00004
Enter a number: 0
The largest number you printed was: 9898.000000
 
So I see that everything inside the do is aligned about a tab stop in, the exception is the while.

I don't think I understand what you are stating here. Can you rephrase it?

Also, see my EDIT #2? What if I had put a semicolon at the end of that if statement instead of commenting it out? What affect does it have on the program?
 
I'm still getting extra 0's in my output. Do I need to specify my float variable to how many decimal places? But I want the user to be able to enter as large a number as they like, then reprint the largest number, but without any extra 0's as baggage.


Code:
Scott-Deans-MacBook-Pro:~ scottdean$ ./a.out
Enter a number: 9898
Enter a number: 74
Enter a number: 39
Enter a number: .00004
Enter a number: 0
The largest number you printed was: 9898.000000

You can restrict the amount with the format specifier. Instead of %f, you type %.2f for two, or %.f for none. But you have to think about the consequence of inputs like 425.33 and 425. 425.33 will still be the highest number but you wont see it, so it's a matter of how accurately you want the information presented only.
 
I don't think I understand what you are stating here. Can you rephrase it?

Also, see my EDIT #2? What if I had put a semicolon at the end of that if statement instead of commenting it out? What affect does it have on the program?

Yeah, no problem, I was simply observing the alignment of your code as I mentioned I wanted to do in my previous post. Just thinking out loud!
 
You can restrict the amount with the format specifier. Instead of %f, you type %.2f for two, or %.f for none. But you have to think about the consequence of inputs like 425.33 and 425. 425.33 will still be the highest number but you wont see it, so it's a matter of how accurately you want the information presented only.

Hmmm, so if I allow the user to enter as large or long a number as they like, and they choose to enter short numbers, then I'm going to get extra 0's in the output? There's no way to allow the user to do what they want, then print exactly whatever the largest number happens to be without the 0's if it happens to be small?

Can I do something ( I know this is beyond my exercise, and I know what I said, but it's a thought that I have) like set a condition that if the largest user_number is x digits then to use a format specifier that matches those digits? Kinda like a switch statement I think?

Case 1 digit printf (%1f)
case 2 digits printf (%2f)

Something like that remotely possible with what i know?
 
Hmmm, so if I allow the user to enter as large or long a number as they like, and they choose to enter short numbers, then I'm going to get extra 0's in the output? There's no way to allow the user to do what they want, then print exactly whatever the largest number happens to be without the 0's if it happens to be small?

Can I do something ( I know this is beyond my exercise, and I know what I said, but it's a thought that I have) like set a condition that if the largest user_number is x digits then to use a format specifier that matches those digits? Kinda like a switch statement I think?

Case 1 digit printf (%1f)
case 2 digits printf (%2f)

Something like that remotely possible with what i know?

i believe %g will achieve what you're looking for.

-Lee
 
Something like that remotely possible with what i know?

There are functions in math.h that lets you split up a float into integer and fraction parts, modf(). Then you need to count the digits in the fraction part, I guess you could use modulus for that in a loop while you add 10 to the number in each iteration or something. There might be some other more clever way to do it though.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.