Thread: Problem with specific command

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    17

    Problem with specific command

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void main(void)
    {
        //declaration
        int Np00000, Ns00000;
        float Vp00000, Vs00000;
        char A, B, C, D, reply;
    
        //intialization
        printf("A : Primary Voltage");
        printf("\nB : Secondary Voltage");
        printf("\nC : Number of Primary windings/coils");
        printf("\nD : Number of Secondary windings/coils");
    
        printf("\n\nWhich of the above component are you finding?(A , B , C or D) : ");
        scanf("%c",&reply);
    
        if (reply == "A")
        {
        printf("This program calculates the Primary Voltage(Vp) of a transformer");
        printf("\nEnter the number of coils in the Primary(Np): ");
        scanf("%d", &Np00000);
        printf("\n\nEnter the number of coils in the Secondary(Ns): ");
        scanf("%d", &Ns00000);
        printf("\nEnter the voltage in the Secondary (in Volts): ");
        scanf("%f", &Vs00000);
        
    
        //calculation
    
        Vp00000 =  Np00000 * Vs00000 / Ns00000;
    
        //output
        printf("\n\nThe Primary Voltage(Vs) is %.2f V", Vp00000);
        }//end of A
    
        if (reply == "B")
        {
        printf("This program calculates the Secondary Voltage (Vs) of a transformer"); 
        printf("\nEnter the number of turns in the Secondary(Ns): ");
        scanf("%d", &Ns00000);
        printf("\n\nEnter the number of turns in the Primary(Np): ");
        scanf("%d", &Np00000);
        printf("\nEnter the voltage in the primary winding(Vp) (in Volts): ");
        scanf("%f", &Vp00000);
    
        //calculation
    
        Vs00000 =  Ns00000 * Vp00000 / Np00000;
    
        //output
        printf("\n\nThe Secondary Voltage(Vs) is %.2f V", Vs00000);
        }//end of B
    
        if (reply == "C")
        {
        printf("This program calculates the number of Primary winding/coils of a transformer"); 
        printf("\n\nEnter the voltage in the Primary winding(Vp) (in Volts): ");
        scanf("%f", &Vp00000);
        printf("\nEnter the number of turns in the Secondary winding/coils: ");
        scanf("%d", &Ns00000);
        printf("\nEnter the voltage in the Secondary winding/coils(Vs) (in Volts): ");
        scanf("%f", &Vs00000);
    
        //calculation
    
        Np00000 = Ns00000 * Vp00000 / Vs00000;
    
        //output
        printf("\n\nThe number of Primary turn is %d", Np00000);
        }
    
        exit(0);
    
    }
    Currently I have this problem at the part of declaring with character A B C or D at the start. For example when I press B , it doesn't go to the if (reply == "B") part of the program , instead it starts with the if (reply == "A") part , causing my whole program about calculating transformer voltage and number of coils to be wrong.

    I know its the part of my declaring that is causing the mistake but I'm clueless about how exactly am i suppose to declare it.

    Hope someone can give me some advice

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use single quotes, for single letters.

    You should be getting compiler warnings when you use double quotes.

    Also, main returns an int, not void.
    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.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Code:
    reply == "A"
    This innocent-looking statement is incredibly wrong. "A" is actually a string consisting of the A character and a null return. Comparing strings using == only compares the pointer (i.e. the memory location of those strings). So unless reply is in the same part of memory as A (not just having the same content), it will return a bad answer. You probably want to use single quotes ('A') which means to compare against a specific CHARACTER (because that's what you read in from scanf).

    To compare strings you require the strcmp functions.

    P.S. Enabling your warnings helps immensely and is said SO OFTEN on these forums that I get bored of keep saying it:

    Code:
    gcc -O3 -g3 -Wall -c -fmessage-length=0 -otest.o ..\test.c
    ..\test.c:5: warning: return type of 'main' is not 'int'
    ..\test.c: In function 'main':
    ..\test.c:21: warning: comparison between pointer and integer
    ..\test.c:21: warning: comparison with string literal results in unspecified behavior
    ..\test.c:40: warning: comparison between pointer and integer
    ..\test.c:40: warning: comparison with string literal results in unspecified behavior
    ..\test.c:58: warning: comparison between pointer and integer
    ..\test.c:58: warning: comparison with string literal results in unspecified behavior
    ..\test.c:10: warning: unused variable 'D'
    ..\test.c:10: warning: unused variable 'C'
    ..\test.c:10: warning: unused variable 'B'
    ..\test.c:10: warning: unused variable 'A'
    As you can see, there are also lots of other problems with your code too.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    17
    Sorry I just start C Programming like a month ago so I'm still unsure about the usage of some commands like the one you said about single quotes. Do I just change void main(void) to int main(void)?? Otherwise what should I do, cause I'm just starting the chapter on branching using "if" and "else". If possible can someone show me a paragraph of commands that will allow me to correct my mistakes

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What ledow was saying is that 'C' is different from "C", the first is a character, the second is a string containing a single character. (ie you would write "ABC", not 'ABC')

    In your code you want to compare the character to another character not to a string, and as previously stated == is not used for string comparison anyway, the function strcmp() is.
    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.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    17
    so i did a small change and it ended up like this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void main(void)
    {
        //declaration
        int Np00000, Ns00000;
        float Vp00000, Vs00000;
        char A, B, C, D, reply;
    
        //intialization
        printf("A : Primary Voltage");
        printf("\nB : Secondary Voltage");
        printf("\nC : Number of Primary windings/coils");
        printf("\nD : Number of Secondary windings/coils");
    
        printf("\n\nWhich of the above component are you finding?(A , B , C or D) : ");
        scanf("%c",&reply);
    
        if (reply == 'A')
        {
        printf("\nThis program calculates the Primary Voltage(Vp) of a transformer.");
        printf("\n\nEnter the number of coils in the Primary(Np): ");
        scanf("%d", &Np00000);
        printf("\n\nEnter the number of coils in the Secondary(Ns): ");
        scanf("%d", &Ns00000);
        printf("\nEnter the voltage in the Secondary (in Volts): ");
        scanf("%f", &Vs00000);
        
    
        //calculation
    
        Vp00000 =  Np00000 * Vs00000 / Ns00000;
    
        //output
        printf("\n\nThe Primary Voltage(Vs) is %.2f V.", Vp00000);
        }//end of A
    
        if (reply == 'B')
        {
        printf("\nThis program calculates the Secondary Voltage (Vs) of a transformer."); 
        printf("\nEnter the number of turns in the Secondary(Ns): ");
        scanf("%d", &Ns00000);
        printf("\n\nEnter the number of turns in the Primary(Np): ");
        scanf("%d", &Np00000);
        printf("\nEnter the voltage in the primary winding(Vp) (in Volts): ");
        scanf("%f", &Vp00000);
    
        //calculation
    
        Vs00000 =  Ns00000 * Vp00000 / Np00000;
    
        //output
        printf("\n\nThe Secondary Voltage(Vs) is %.2f V.", Vs00000);
        }//end of B
    
        if (reply == 'C')
        {
        printf("\nThis program calculates the number of Primary winding/coils of a transformer."); 
        printf("\n\nEnter the voltage in the Primary winding(Vp) (in Volts): ");
        scanf("%f", &Vp00000);
        printf("\nEnter the number of turns in the Secondary winding/coils: ");
        scanf("%d", &Ns00000);
        printf("\nEnter the voltage in the Secondary winding/coils(Vs) (in Volts): ");
        scanf("%f", &Vs00000);
    
        //calculation
    
        Np00000 = Ns00000 * Vp00000 / Vs00000;
    
        //output
        printf("\n\nThe number of Primary turn(s) is %d.", Np00000);
        }//end of C
    
        if (reply == 'D')
        {
        printf("\nThis program calculates the number of Secondary winding/coils of a transformer."); 
        printf("\n\nEnter the voltage in the Secondary winding(Vs) (in Volts): ");
        scanf("%f", &Vs00000);
        printf("\nEnter the number of turns in the Primary winding/coils: ");
        scanf("%d", &Np00000);
        printf("\nEnter the voltage in the Primary winding/coils(Vs) (in Volts): ");
        scanf("%f", &Vp00000);
    
        //calculation
    
        Ns00000 = Np00000 * Vs00000 / Vp00000;
    
        //output
        printf("\n\nThe number of Secondary turn(s) is %d.", Ns00000);
        }//end of D
    
        exit(0);
    
    }
    Looks like the program is working fine now

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is good to hear. One thing you can do to improve your code is to indent it better. You have one level of indentation in the main function, but the code within the body of each if statement should be indented by another level.

    You should specify the return type of main to be int, not void. Instead of exit(0) at the end of main, just use return 0;

    Also, since you are not using the char variables named A, B, C and D, you should remove their declarations. Then, it may make more sense to rename Vp00000 to just Vp or better yet, primary_voltage (or something like that). If you do need more such variables later, chances are you would be using an array so the 00000 is pointless.
    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

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    17
    I wasn't taught to use int as main and return 0

    But its pretty nice to see someone from the same country as me , Singapore , who is able to help me out. Thanks anyway (:

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Muppetlol View Post
    I wasn't taught to use int as main and return 0
    You've been taught to now...
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem creating program to the specific specification
    By rushhour in forum C++ Programming
    Replies: 22
    Last Post: 11-28-2008, 12:15 AM
  2. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  3. Input From a Text File - A Specific Problem.
    By Eddie K in forum C Programming
    Replies: 4
    Last Post: 03-09-2006, 03:50 PM
  4. MSVC++ specific problem
    By HybridM in forum C++ Programming
    Replies: 9
    Last Post: 07-29-2003, 07:13 AM
  5. Replies: 2
    Last Post: 01-02-2002, 01:42 PM