Thread: for loop problems

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    30

    for loop problems

    hey guys can some one look over my code....it wont compile im using miracle c...can you guys tell me if its the code or the compiler....thanks

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
    
    	/* declared variable*/
    	int a;
    	int b;
    	int c;
    	int d;
    	
    	
    	for(a = 1; a <= 7; a++) // will increment by 1 until 7 is reached
    	{
    		printf("%d/n", a); //will print out result
    	}
    	
    	
    	for(b = 1; b <= 23; b += 5)
    	{
    		printf("%d/n", b);
    	}
    	
    	for(c = 20; c >= -10; c -= 6)
    	{
    		printf("%d/n", c);
    	}
    		
    	for(d = 19; d <= 51; d += 8)
    	{
    		printf("%d/n", d);
    	}
    	
    	
    	getch();
    	return 0;
    	
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are the compile errors?

    It looks fine, except that you probably should use /* */ style comments instead of // style comments, and getchar() instead of getch().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    there are no compile errors it just wont compile....is there a free c compiler out there?? thats better then miracle c?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    there are no compile errors it just wont compile....
    Have you ever gotten it to compile before?

    is there a free c compiler out there?? thats better then miracle c?
    You can use g++ (or its MinGW port on Windows) or the compiler from Visual C++ Express.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    i fixed the ch to char it ran and its good now thanks

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yes, try: dev c++, visual studio express, or code blocks.

    Also, getch() should be getchar(). Maybe that would get it to work? Although your compiler should say something about it.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    it did work i have dev c++ but i dont know how to compile c programs in it...i would write the code save it as a c file but it wont compile or anything....and i have to put getchar at the end of the file because i run in vista and the dos window wont stay up

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by clezzie View Post
    an you guys tell me if its the code or the compiler.
    99.999% of the time it's the code

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Also I am sure you meant to place a new line? Good call on using getchar

    use this

    Code:
    printf("%d\n", b);

    Also not sure why you need the getchar() here?

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    getchar() is what keeps the command line window from disappearing at the end of program execution.

    Also, you not not need the math header file in the code.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Prefer using "/* */" for comments instead of "//" in ANSI standart it is not acceptable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM