Jump to content



Photo

do...while printing numbers 1 though 9


  • Please log in to reply
8 replies to this topic

#1 Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 21 September 2013 - 19:45

Hello everyone!

 

I'm writing code that prints the numbers 1 though 9 (Including 9).  This is the code I have:

#include "stdio.h"

void main()
{
	int count;
	count = 1;

	{
		do 
		(count = count + 1);

		while
		(count < 10);
			printf("%d", count);
	printf("\n");
	}
}

However when I run this it is not printing the numbers 1 through 9, its only printing out the number 10.  Can someone please help me?  I really don't know what I'm doing wrong and I am unfamiliar with how to use do ...while loops in c.

 

 

Attached Images

  • failed 1c printout.png



#2 zman982

zman982

    You can not harm me!

  • Joined: 13-March 04
  • Location: Jackson, MS
  • OS: Windows 8.1 Pro x64
  • Phone: Galaxy Note 2

Posted 21 September 2013 - 19:57

Two things I see you need to do.

 

First move the statement printf("%d", count); after count = count+1 and before the while since right now it's not in the loop.

 

Second you need to set count= 0, because if you leave it at 1 and start to run to program it's going to print 2 first since you have the statement count +1 which will set count to 2 the first time through the loop.



#3 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 21 September 2013 - 20:03

The problem you are trying to solve is trivial in virtually any programming language. It would probably behoove you to read a beginner's book that teaches basic programming logic intermixed with lessons in a specific programming language. Once you understand one language - and more importantly have a good grasp on how to program - learning other languages becomes much easier. Therefore I strongly recommend against starting with C as your first language. Although C is very powerful, it lacks the object-orientation that is of paramount importance in most other high-level languages and has far more caveats that make it very difficult to become a proficient C programmer. Personally I would recommend Python as a good first language in conjunction with the book Learn Python The Hard Way.

 

That said, zman982's analysis of your mistake is correct. You are printing the count after it has reached 10, not as you are iterating like you apparently intended. A do... while loop does not make much sense in this case anyway. It would be much more concise to use a for loop instead.

#include <stdio.h>

int main( int argc, char * argv[] )
{
    for( int i = 1; i < 10; i++ ) printf( "%d\n", i );
    return 0;
}


#4 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 21 September 2013 - 20:06

+xorangekiller, I would love to agree with you about not starting with C, however the institution of higher education that I am in believes that once you take SQL, algorithms and data structures, and VB.NET that you have to take C to get into java/flash.  I have already completed this successfully using a for loop, and a while loop, however this do loop is throwing me for a loop (LOL). 



#5 Descartes

Descartes

    Neowinian

  • Joined: 29-November 08
  • Location: Poland

Posted 21 September 2013 - 20:07

There are several ways you can construct a loop in C, one of which is a for loop proposed by xorangekiller. However, if you really want to use a do .. while loop, the syntax is

do
{
    // loop body
}
while (condition)

While omitting the curly braces is possible in certain conditions, I'd advise against it, as at this point it will only confuse you, and it's generally considered to be bad style.

 

Remember that this loop, as opposed to while or for will always execute at least once - it performs the do part first, then checks if it should repeat.

 

Other problems that I've found with your code:

#include "stdio.h"

should be

#include <stdio.h>

Your compiler probably fixed it automatically for you, but most of the time you use <file> for built-in headers, and "file" for those included in your project.

void main()
{
    // code
}

should also be fixed to

int main()
{
    // code

    return 0;
}

Again, this is something that your compiler might have fixed, but in Unix environment, it's a standard behaviour that an application returns 0, when it completes without errors.



#6 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 21 September 2013 - 20:18

If you must learn C now, I recommend that you read the C Programming Language (Second Edition). It was written by the original authors of the C language and is by far one of the best programming books I have ever read. It very thoroughly explains the why and how of C from the author's point-of-view to help you gain better a understanding of the language. That is why it sits on my bookshelf as a prominent member of my reference collection.



#7 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 21 September 2013 - 20:25

Ok fellas thank you for all of your help and wonderful insight!  You have been a tremendous help.  I followed your suggestions zman982 and it is working flawlessly!  Thank you so much for everything! 



#8 vcfan

vcfan

    Straight Ballin'

  • Tech Issues Solved: 3
  • Joined: 12-June 11

Posted 21 September 2013 - 20:34

 
#include <stdio.h>
 
int main(int argc, char* argv[])
{
    int count = 0;
    
    do {
         count++;
         printf("%d\n", count); }
    
    while(count < 9);


    return 0;
}
 
 
 


#9 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 21 September 2013 - 22:33

I really don't know what I'm doing wrong and I am unfamiliar with how to use do ...while loops in c.

If you are working in Visual Studio (which you likely are since you're on Windows), start your program by pressing F10 (step over) and press F10 repeatedly to see program executing line by line. You can see the value of the different variables in your program simply by hovering the mouse cursor over them. This will allow you to visualize exactly what your program does. The earlier you learn to use the debugger, the faster you'll learn programming.

 

Here's a good tutorial on debugging: http://www.codeproje...o-2010-A-Beginn