0

Basically, I'm trying to create a program where you can read up to 100 inputs from the keyboard. The inputs must be numbers between [1,100] and when "0" is entered, the program exits the loop and displays an array of all the numbers entered except the number zero. I'm trying to work with "while,for,if" statements but it's a bit difficult to understand the logic. Could you guys help me and give me some tips and feedback? Sorry for my ignorance but I'm programming for the first time.

#include <stdio.h>

int main(){

    int long ship;
    int array[100];
    
    for(ship = 0; ship < 100; ship++){
        printf("Repaired ship: ");
        scanf("%li", &ship[array]);

        while(ship[array] == 0){
            printf("\n\t");
            for(ship = 0; ship < 100; ship++)
                printf("%li ", ship[array]);
            printf("\n\nRepaired ships: %li", ship);
        }
    }

    if(ship == 100){
        printf("\n\t");
        for(ship = 0; ship < 100; ship)
            printf("%li ", ship[array]);
        printf("\n\nRepaired ships: %li", ship);
    }
    return 0;
}
rdtsc
  • 303
  • 2
  • 10
Basick
  • 41
  • 2
  • This is a very common question. Here is an answer that explains how to safely handle input data from users in C. https://stackoverflow.com/questions/9278226/which-is-the-best-way-to-get-input-from-user-in-c – Fra93 Oct 30 '22 at 12:10
  • Where you have `ship[array]` it is more usual to have `array[ship]` – Weather Vane Oct 30 '22 at 12:12
  • ... in the idiomatic notation `array[ship]` it is easier to see that `"%li"` is the wrong format specifier, because the array is type `int`. It must be `"%d"` or swap the types of `ship` and `array`. – Weather Vane Oct 30 '22 at 12:18
  • The nested `ship` loop conflicts with the outer `ship` loop, becaue `ship` is the same variable. – Weather Vane Oct 30 '22 at 12:28
  • I will certainly improve. I think I have the working logic, my main problem is to understand the correct use of the programming language. Thanks guys! – Basick Oct 30 '22 at 15:21

1 Answers1

2

I have edited your code with some minor corrections. In the comments below the code, I help clarify the changes.

Code:

#include <stdio.h>

int main(){

    int ship;  
    int long array[100]; 

    for(ship = 0; ship < 100; ship++)
    {
        printf("Repaired ship: ");
        scanf("%ld", &array[ship]);   

      if(array[ship]==0)  
          break;
    }

    ship=0;  

    while(array[ship] != 0) 
    {
        printf("\n\t");

        printf("%ld ", array[ship]);
        ship++;
    }

    printf("\n\nRepaired ships: %d", ship);
        
    return 0;
}
  • The long int is not needed for ship as 100 is not that big of a value.
  • If required, the long int will be used for array.
  • The format specifier %ld is required for long int.
  • The for loop handles the input.
  • We don't need a for loop inside the while as it already deals with the output.
  • The use array[ship] type notation instead of ship[array] as the former is more common.

This if statement is not needed:

if(ship == 100){

    printf("\n\t");

    for(ship = 0; ship < 100; ship)

        printf("%li ", ship[array]);

    printf("\n\nRepaired ships: %li", ship);

}
James Risner
  • 5,451
  • 11
  • 25
  • 47
  • Thank you for the help Abhinav! Can you explain to me why you set ship to zero value? ship=0; – Basick Oct 30 '22 at 15:19
  • You're welcome Basick! I have used ship=0 because when the for loop terminates then it terminates with ship value changed to position where you typed zero. Eg. if you typed 0, at 20th input,then ship=20. But when you print the output, you must begin with starting of array ;so we initialsied ship with zero(i.e. ship=0). – Abhinav Ojha Oct 30 '22 at 17:12
  • Oh, I get it! Your explanation was really useful! Thankss – Basick Nov 01 '22 at 16:55