0

im trying to do an assignment that deals with planes and repairs.

Im trying to scan in a list of an int with a string, for instance:

1 yes

2 no

3 no

and so on. However, whenever the number -1 is typed, everything has to stop. However, with the code I have now, when -1 is typed, the code still expects a yes or no to come after. How do I solve this so after -1 is typed, everything terminates?

This is my code so far:

int main(int argc, char *argv[]) {
    
    int number = 0;
    char YesOrNo[4];

    scanf("%d", &number);
    while (number != -1) {
        scanf("  %s", YesOrNo);
        printf("%d %s\n" , number, YesOrNo);
    }

    
    return 0;
}

tried to code it with while loops etc

Justus148
  • 1
  • 1
  • Your code is missing a second scan of `number` so add `scanf("%d", &number);` after the `printf` – Support Ukraine Feb 12 '23 at 14:14
  • OT: `scanf(" %s", YesOrNo);` --> `scanf("%3s", YesOrNo);` or better: `if (scanf("%3s", YesOrNo) != 1) exit(1);` – Support Ukraine Feb 12 '23 at 14:15
  • And `scanf("%d", &number);` --> `if (scanf("%d", &number) != 1) exit(1);`; – Support Ukraine Feb 12 '23 at 14:16
  • See https://ideone.com/rOnjg4 – Support Ukraine Feb 12 '23 at 14:35
  • Thing #1: Always check the return value from `scanf`, to see if it was able to successfully scan as many values as you asked it to. – Steve Summit Feb 12 '23 at 14:47
  • Thing #2: Despite its widespread popularity, `scanf` is rather underpowered and difficult to use, especially for problems of the form "Enter input as long as it's X, but then use Y to terminate". When you're ready to graduate beyond it, see [What can I use for input conversion instead of `scanf`?](https://stackoverflow.com/questions/58403537) – Steve Summit Feb 12 '23 at 14:48

4 Answers4

1

This might work for what you are trying to do. If I understood correctly, you want to exit when, instead of a regular string, the user types "-1".

#include <stdio.h>

int main(int argc, char *argv[]) {

    int number = 0;
    char YesOrNo[4];

    printf("Loop starts");
    while (number != -1) {
        scanf("  %s", YesOrNo);
        if (YesOrNo[0] == '-' && YesOrNo[1] == '1' && YesOrNo[2] == 0) {
            number = -1;
        }
        printf("%d %s\n" , number, YesOrNo);
    }
    printf("Exit loop\n");

    return 0;
}

BTW: This code is not buffer overflow safe. You would need to limit the amount of chars it can read. Adding a number to scanf, like %3s is enough to not get buffer overflow, but might not work for your application.

Dioswison
  • 81
  • 9
  • @AdrianMole Because *YesOrNo was easier than YesOrNo[0] and also i don't really think YesOrNo[1] is easier to write. Yes it is easier to read, but i got used to pointers for strings because it is easier to iterate when you don't know the size (When you iterate until a zero) – Dioswison Feb 15 '23 at 20:50
  • @AdrianMole I don't really get your point, *(a+i) is part of C syntax (and as you said valid) – Dioswison Feb 15 '23 at 22:20
  • Yeah, no problem changin it, but whay is the issue with indirection operation over array index? – Dioswison Feb 15 '23 at 22:45
  • 1
    I got it after reading it a couple of times, you mean i should use array specific notation to access arrays – Dioswison Feb 15 '23 at 22:47
1

First thing that input for number is outside the while loop. So, practically you are not taking the number input after that. Second is that you can add a if condition in the while loop to break the while loop.
Below are the changes, you should make in your code.

int main(int argc, char *argv[]) {
    
    int number = 0;
    char YesOrNo[4];

    while (number != -1) {
        scanf("%d", &number);
        if(number==-1){
            break;
        }
        scanf("  %s", YesOrNo);
        printf("%d %s\n" , number, YesOrNo);
    }

    
    return 0;
}

Hope this helps! ✌️

0

In your code, the input of the number is outside the loop, so it is executed only once.

I think the clearest way to express your input format is an infinite loop. Use break to finish execution.

int main(int argc, char *argv[])
{
    int number = 0;
    char YesOrNo[4];

    while (1)
    {
        scanf("%d", &number);
        if (number == -1)
            break;
        scanf("  %s", YesOrNo);
        printf("%d %s\n" , number, YesOrNo);
    }

    return 0;
}

As other people mentioned, this code still has problems (buffer overflow; error handling). But the input format should work now.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
0

You have to take a int input and a string input in each iteration. Which means both your scanf should be inside the while loop. And if the int input is -1 then you have to terminate the loop immediately. Here's the corrected code:

int main(int argc, char *argv[]) {

int number = 0;
char YesOrNo[4];


while (number != -1) {
    scanf("%d", &number);
    if(number==-1)
          break;
    scanf("  %s", YesOrNo);
    printf("%d %s\n" , number, YesOrNo);
}


return 0;

Hope this helps! :D