Thread: New to C, having problems.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    UK
    Posts
    2

    New to C, having problems.

    Hello everybody, I'm new to this forum, hopefully it doesn't shine a bad light on me that my first post is one asking for help...

    Anyway, I'm quite new to C, but can normally manage to work around any small problems I have on my own, however today my brain doesn't seem to be functioning. I can't figure out what's wrong here:

    Code:
    #include "aservelibs/aservelib.h"
    #include <stdio.h>
    #include <math.h>
    
    /**/
    
    int main ()
    {
    	float frequency;
    	scanf("%f", &frequency);
    	while (frequency <= 20000) 
    	{
    		printf("frequency");
    		aserveOscillator(0, frequency, 1.0, 0);
    		aserveSleep(500);
    	}
    	else
    	{
    		aserveOscillator(0, 0, 0, 0);
    	}
    
    	
    	return 0;
    }
    I was wondering if anyone could point me in the right direction?
    Thanks in advance for any help.

    I'm a fool. Please disregard this post!
    Looked back at it after a short break and kicked myself. Oh well.
    Last edited by spark_ey; 11-08-2010 at 11:54 AM. Reason: Fixed the problems.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Welcome sparkey!

    Well could you at least describe what is going on? What seems to be wrong?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The else follows while?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There is no while-else construct in C. I'm not totally clear on what you're trying to do, but you probably want some sort of if-else block inside the while loop, or a while loop in one or both of the if-else sections:

    Code:
    while (frequency < 20000) {
        if (some condition) {
            ...
        }
        else {
            ...
        }
    }
    Code:
    if (frequency < 20000) {
        while (some condition) {
            ...
        }
    }
    else {
        ...
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    UK
    Posts
    2
    Apologies for not explaining myself very well-if at all.

    I'm trying to build a program to do as follows:

    Start>Initialise frequency variable>(Start loop)Is frequency variable less that 20,000?(If no, end)>Set oscillator to frequency variable value>Display frequency value>Add 50 to the frequency variable>Pause(loop back to "Is frequency variable less that 20,000?">Stop

    Hopefully that makes it slightly more clear.

    As I mentioned, I'm very new to C and, honestly, am not particularly good at it.

    http://img155.imageshack.us/img155/2...01108at180.png - a screenshot of the flowchart I'm working to.
    Last edited by spark_ey; 11-08-2010 at 12:11 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    while loops don't have else clauses

    while loops should CHANGE the expression in some way, unless you intend to loop forever.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  2. tile colliision detection problems
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 10-23-2002, 09:26 PM
  3. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM
  4. Problems with my RPG app
    By valar_king in forum Game Programming
    Replies: 1
    Last Post: 12-15-2001, 08:07 PM
  5. problems with too many warning messages?
    By Isometric in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 01:23 AM