This is a very small part of our college groupwork. We’d have to present our code and explain how each part of the code works.
We wanted to ensure that the user enters an integer and prompt the user to reenter if they didn’t enter a valid number.
We managed to come up with a solution, but I can’t figure out why it works.
While attempting to come up with a solution, we figured using
numberchecker==input\[i\]
if(!(numberchecker==input\[i\]))
However, it does the opposite of what we wanted so I removed the ‘!‘ and now it somehow works? Why/how is this happening? Shouldn’t the if statement only trigger if we enter an integer?
#include <stdio.h>
int main()
{
int i,x,input[128],answer,numberchecker;
printf("Input number of numbers: ");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("Input number %d:",i+1);
scanf("%d",&input[i]);
numberchecker==input[i];
if(numberchecker==input[i])
{
do
{
printf("\nInvalid number! Please try to input a valid number\n\nInput number %d:",i+1);
scanf("%d",&input[i]);
numberchecker==input[i];
}
while(numberchecker==input[i]);
}
}
}