Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Homework in C - Help needed

Ondrej_frajer

New Coder
Hey, I was accepted to the University for programming major, even though I basically never programmed anything in my life before, however my first homework here is to make a program in which I write 3 coordinates ([x, y]) and the program should be able to calculate 3 more coordinates so with one of the new coordinates and the 3 I wrote there, it would make a rectangle, or any of its special form (square, rhombus, parallelogram) and then the program should decide whether the newly formed tetragonal is square, etc. However, I need the program to work even when I fill in the coordinates in these forms

[ 5, 0 ]

or when it’s all written in one line only

[10.5, 10.5]
[12.5, 10.5][10.5, 15e+0]

or when I write it like this [3,

4

])

but my program automatically says it’s incorrect input and I can’t figure out how to make that work, I even tried ai, but it didn’t help me neither, so any help from a more experienced programmer would be greatly appreciated. Everything else should work just fine. I hope that my problem is understandable as English isn’t my first language

Here is the part of my program, where I fill in the coordinates:

"Bod A" means coordinate A in my language and so on, and "Nespravny vstup" means incorrect input

C:
int main(void) {
    double ax, ay, bx, by, cx, cy;
    double tolerance = 0.0000001;
    char input[100];

    printf("Bod A:\n");
    fgets(input, sizeof(input), stdin);
    if (sscanf(input, "[%lf, %lf]", &ax, &ay) != 2) {
        printf("Nespravny vstup.\n");
        return 1;
    }
    
      printf("Bod B:\n");
    fgets(input, sizeof(input), stdin);
    if (sscanf(input, "[%lf, %lf]", &bx, &by) != 2) {
        printf("Nespravny vstup.\n");
        return 1;
    }

    printf("Bod C:\n");
    fgets(input, sizeof(input), stdin);
    if (sscanf(input, "[%lf, %lf]", &cx, &cy) != 2) {
        printf("Nespravny vstup.\n");
        return 1;
    }
 
Hey, congrats on getting into the programming major! I see what you're trying to do with your code, and I understand why it's giving you problems with the different input formats.

The issue is that your sscanf pattern ("[%lf, %lf]") is too strict, so it expects input exactly like [x, y]. This won't work with inputs that have extra spaces, newlines, or different formats.

To handle multiple input styles, you could try cleaning the input a bit before parsing it, removing any unnecessary spaces or newlines. Here's a suggestion:

1. Remove extra spaces, line breaks, and odd formatting.
2. Use a more flexible sscanf pattern.

Here's how you could tweak your input section:

Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void clean_input(char* input) {
    int i, j = 0;
    for (i = 0; input[i]; i++) {
        if (!isspace(input[i])) {  // Remove spaces, newlines
            input[j++] = input[i];
        }
    }
    input[j] = '\0';
}

int main(void) {
    double ax, ay, bx, by, cx, cy;
    char input[100];

    printf("Bod A:\n");
    fgets(input, sizeof(input), stdin);
    clean_input(input);
    if (sscanf(input, "[%lf,%lf]", &ax, &ay) != 2) {
        printf("Nespravny vstup.\n");
        return 1;
    }

    printf("Bod B:\n");
    fgets(input, sizeof(input), stdin);
    clean_input(input);
    if (sscanf(input, "[%lf,%lf]", &bx, &by) != 2) {
        printf("Nespravny vstup.\n");
        return 1;
    }

    printf("Bod C:\n");
    fgets(input, sizeof(input), stdin);
    clean_input(input);
    if (sscanf(input, "[%lf,%lf]", &cx, &cy) != 2) {
        printf("Nespravny vstup.\n");
        return 1;
    }

    // Continue with the rest of your program
}

The clean_input function strips spaces, line breaks, and other unnecessary characters, so that your sscanf can work more smoothly. This should help you handle those different formats like the ones you mentioned.

Give that a try and see if it works better! Let me know if you run into more trouble.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom