Thread: Wierd sign in my program (beginner)

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    3

    Wierd sign in my program (beginner)

    Hi everyone,

    This is my first post on here. I am currently following a c programming introduction course (I don't have any experience in programming). I am programming in Kate on Linux (virtual machine).

    I am currently working on making a basic calculator. I am getting there, I am only missing a function to convert a 16 bit binary imput to decimal value, doing research on how to do that best now.

    However, I am getting something wierd when I execute the program. There is a � sign before "Result: x". Sometimes it is not a � but a '.' or '/'. I am not sure how it can be removed. Any help would be great!

    PS.
    I know that this program can be written in fewer lines, however my priority is on making a working program first.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int b, x;
        float res, a;
        char c, bord;
        res = 0;
        
        printf("** Calculator **\n");
        
        while (c != '=')
        {
        printf("Binary or decimal? (b/d) ");
        scanf(" %c", &bord);
        switch(bord)
        {
            case 'd':   printf("Value? ");
                        scanf(" %f", &a);
                        printf("Input: %.0f\n", a);
                        printf("Operation? ");
                        scanf(" %c", &c);
                        printf(" %c", &c);
                        switch(c)
                        {
                            case '+':   res = a + res;
                                        break;
                            case '-':   res = res - a;
                                        break;
                            case '/':   res = res / a;
                                        break;
                            case '*':   res = a * res;
                                        break;
                            case 'c':   res = 0;
                                        break;
                            default:    res = res;
                                        break;   
                        }
                        break;
            //Make a function for converting binary values
            /*case 'b':   printf("Value? ");
                        scanf(" %f", &a);
                        x = read_binary_value();
                        printf("Input: %f.0\n", x);
                        printf("Operation? ");
                        scanf(" %c", &c);
                        printf(" %c", &c);
                        switch(c)
                        {
                            case '+':   res = x + res;
                                        break;
                            case '-':   res = res - x;
                                        break;
                            case '/':   res = res / x;
                                        break;
                            case '*':   res = x * res;
                                        break;
                            case 'c':   res = 0;
                                        break;
                            default:    res = res;
                                        break;   
                        }
                        break;*/
            default:    a = 0;
                        printf("Operation? ");
                        scanf(" %c", &c);
                        printf(" %c", &c);
                        switch(c)
                        {
                            case '+':   res = a + res;
                                        break;
                            case '-':   res = res - a;
                                        break;
                            case '/':   res = res / a;
                                        break;
                            case '*':   res = a * res;
                                        break;
                            case 'c':   res = 0;
                                        break;
                            default:    res = res;
                                        break;   
                        }
                        break;
                                
        }
        printf("Result = %.2f\n", res);
        }  
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    When you're printing a single character with "%c", you don't need and don't want ambersand( & ) there. Lines #23 and #67.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    3
    Quote Originally Posted by GReaper View Post
    When you're printing a single character with "%c", you don't need and don't want ambersand( & ) there. Lines #23 and #67.
    Thank you! I updated it, now the wierd sign is gone. However I am still getting my operator sign printed there, like so:

    Code:
    ** Calculator **
    Binary or decimal? (b/d) d
    Value? 5
    Input: 5
    Operation? +
    +Result = 5.00
    When i add \n after the print function for the operator, I get the following:

    Code:
    ** Calculator **
    Binary or decimal? (b/d) d
    Value? 5
    Input: 5
    Operation? +
    +
    Result = 5.00
    This suggest (to me at least) that the malfunction is somewhere between that print function and the result. I can't find it this.

    I have also tried placing the print function of the result infront of the last break, but that did not change anything.

    Do you know how I can remove the 2nd operator sign?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > However I am still getting my operator sign printed there, like so:

    Why does this confuse you?
    printf("Operation? ");
    scanf(" %c", &c);
    printf(" %c", c); // This prints it!
    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.

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    3
    Oh damn, nevermind. Thanks haha, that was stupid of me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program ouputs wierd results
    By sanddune008 in forum C Programming
    Replies: 6
    Last Post: 03-01-2010, 09:19 PM
  2. Replies: 0
    Last Post: 11-19-2009, 03:00 AM
  3. Please Help Me with this beginner C++ Program!
    By ClearSights in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 10:22 AM
  4. c program , kinda wierd
    By agarwaga in forum C Programming
    Replies: 2
    Last Post: 04-17-2006, 06:43 AM
  5. Replies: 5
    Last Post: 06-15-2003, 11:20 AM

Tags for this Thread