Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C program only takes input for the first prompt

My program only takes input for the first prompt and it excludes the succeeding ones, and instead, it just directly prints them.

#include <stdio.h>

int main(){
    
    // Initialize
    char cCurrencyA, cCurrencyB;
    float fCurrencyA;
    float fRate;
    
    // Prompt user for currency names
    printf("Enter a currency name: ");
    scanf("%c%c", &cCurrencyA, &cCurrencyB);
    
    // Prompt user for the current rate
    printf("Enter the rate:");
    scanf("%f", &fRate);
    
    // Prompt user to enter value for currency A
    printf("Enter value for %c", cCurrencyA);
    scanf("%f", &fCurrencyA);
    
    // Convert currency A to B
    int nResult = fCurrencyA * fRate;
    
    // Print the result
    printf("%f%c is %d%c", fCurrencyA, cCurrencyA, nResult, cCurrencyB);
    return 0;
    
}

Output:

Enter a currency name:USD PHP
Enter the rate:
Enter value for U:
0.000000U is 0S

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  1. so you have major problems , you are scanning a whole string into only one char , that’s wrong , you should do char cCurrencyA[20], cCurrencyB[20]; not char cCurrencyA, cCurrencyB; to scan a string not only one char.

  2. when you are scanning using scanf(), use the quantifier %s not %c , as %c will get only one character but %s will scan a whole string.

  3. also to round the result , you could use math header file , by using the function called lround() , so do this int nResult = round((double)fCurrencyA * fRate);
    not int nResult = fCurrencyA * fRate; as the lValue is int while RValue is float so you have to cast it.

and here the code edited :

#include <stdio.h>
#include <math.h>
int main(){

    // Initialize
    char cCurrencyA[20], cCurrencyB[20];
    float fCurrencyA;
    float fRate;

    // Prompt user for currency names
    printf("Enter a currency name: ");
    scanf("%19s%19s", &cCurrencyA, &cCurrencyB);

    // Prompt user for the current rate
    printf("Enter the rate:");
    scanf("%f", &fRate);

    // Prompt user to enter value for currency A
    printf("Enter value for %s", cCurrencyA);
    scanf("%f", &fCurrencyA);

    // Convert currency A to B
    int nResult = lround((double)fCurrencyA * fRate);

    // Print the result
    printf("%f%s is %d%s", fCurrencyA, cCurrencyA, nResult, cCurrencyB);
    return 0;

}

and here is the output :

Enter a currency name:USD PHP
Enter the rate:10.5
Enter value for USD10
10.000000USD is 105PHP
Process finished with exit code 0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading