How to avoid gibberish?

This is a discussion on How to avoid gibberish? within the C Programming forums, part of the General Programming Boards category; I am learning C Programming... Now that I am in the process of writing programs, I have a doubt which ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    47

    How to avoid gibberish?

    I am learning C Programming...


    Now that I am in the process of writing programs, I have a doubt which I am not able to clear yet.
    I tried searching the internet for that stuff without any luck.

    I am posting a very simple program here.
    The Program works absolutely fine. The only thing is that it picks up some gibberish.

    My question is...is there any hard and fast rule to avoid getting this gibberish? If yes, what is it?

    Here's the Program:

    Code:
    /*Lara's basic salary is input through the keyboard.
     Her dearness allowance is 35% of basic salary, and house rent allowance is 
     25% of basic salary. Write a program to calculate his gross salary. */
    
    #include<stdio.h>
    main ()
    {
    	int basic;
    	float gross;
    	
    	printf("Enter your Basic Salary:%d");
    	scanf("%d", &basic);
    
    	gross = basic+(0.35*basic)+(0.25*basic);
    
    	printf("Your Gross Salary is:  %f",gross);
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    printf("Enter your Basic Salary:%d");
    remove the %d

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    LOL...that was funny!

    Also, are there any other cases that I would have to check if I get gibberish?

    I guess I found one such condition!

    Thanks.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by duffmckagan
    Also, are there any other cases that I would have to check if I get gibberish?

    I guess I found one such condition!
    I don't get you. Please explain.

  5. #5
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Quote Originally Posted by noodles
    remove the %d
    I will explain. %d is a conversion character for printf. It holds the place for a variable which you put in at the end. For example, to print out the value of the variable num, the code would be
    Code:
    printf("%d", num);
    The %d holds the place and then the variable is an argument at the end. Your code uses %d but does not give a variable to print. This means that it just takes a number from anywhere and prints it. Just removing the %d will fix this.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    Got it...

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    Also enlighten me if there are any other such conditions you can think of...that may lead to gibberish...

    Thanks...

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Quote Originally Posted by duffmckagan
    Also enlighten me if there are any other such conditions you can think of...that may lead to gibberish...

    Thanks...
    There's tons of ways to end up with gibberish, and they all come from misuse of your memory space. Using unitialized/dangling pointers and out-of-bounds array accesses, are both major causes of gibberish.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Your "gibberish" is some random value from your computer RAM.

    This may also lead to "gibberish", unless you had intended it to happen!

    Code:
    printf("%d", rand());
    Gibberish is in the eye of the beholder. One man's gibberish may be another man's gold.
    </prose>
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. priority inversion due to pthread_mutex_lock(): how to avoid?
    By mynickmynick in forum C Programming
    Replies: 11
    Last Post: 04-07-2009, 11:34 AM
  2. Using inFile to read from TXT yields gibberish
    By Zzaacchh in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2008, 08:59 PM
  3. SQL: When to Avoid Index?
    By alphaoide in forum Tech Board
    Replies: 3
    Last Post: 12-02-2006, 01:25 PM
  4. How to avoid a flickring in XOR_PUT
    By planet_abhi in forum Game Programming
    Replies: 3
    Last Post: 10-02-2003, 03:49 PM
  5. How to avoid template ambiguity
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2002, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21