0

I'm not that good at English, but I'm wondering why my code for loop section is using printf twice instead of only once when scanf stops it? I use for loop just because I like to make it similar to for loop on top of it

#include <stdio.h>
#include <stdlib.h>

float findMax(float * number);
float findMin();
float findSum();
float sortVector();


int main()
{       
    float number[7];
    char choice;
    
    printf("Enter 7 number into the vector: ");
    
    for(int i = 0; i < 7; i++)
    {
        scanf("%f", &number[i]);
    }
    
    for( ; ; )
    {
        
        printf("#################### MENU ####################\nA. Find the maximum number in the vector\nB. Find the minimum number in the vector\nC. Find the total of number in the vector\nD. Sort number in the vector in the ascending order\nQ. Quit Program\nEnter your chioce <A, B, C, D or Q> : ");
        scanf("%c", &choice);
        
        switch(choice)
        {
            case 'A':
                printf("The total of numbers in the vector is %.2f", findMax(number));
                    break;
            case 'a':
                printf("The total of numbers in the vector is %.2f", findMax(number));
                    break;
            case 'B':
                
                    break;
            case 'b':
                
                    break;
            case 'C':
                
                    break;
            case 'c':
                
                    break;
            case 'D':
                
                    break;
            case 'd':
                
                    break;
            case 'Q':
                exit(0);
                    break;
            case 'q':
                exit(0);
                    break;      
        }
    }
    
    return 1;
}

float findMax(float * number)
{
    float max;
    
    return max;
}

This is what my cmd shown

Do I miss something or do I code it wrongly?

P.S. This work is not finished yet and I'm just a beginner at coding.

Nick
  • 138,499
  • 22
  • 57
  • 95
  • Your scans of the floating point numbers is leaving a newline in the buffer, which is being read by `scanf("%c", &choice);` and causing your loop to run twice before requesting input. Changing to `scanf(" %c", &choice);` as explained in the dupe should solve it. – Nick Oct 22 '22 at 05:20
  • Note that you should always tag your question with the programming language (`c`) to ensure the largest number of people see it. – Nick Oct 22 '22 at 05:21
  • Thanks for helping me and suggest me what to add more to my tag. – LunaticCoder Oct 22 '22 at 05:32

0 Answers0