Alright, I'm just starting to learn C, and I've run into a problem. This code, which is adapted from a book I'm using, isn't working correctly.
#include <stdio.h>
int main ()
{
char idFound = 0;
int counter; //Used for sequential search for loop, and for getting balance from the second array, which is parallel
int enteredID; // The ID that the user entered, to be searched
int id[5] = {1,2,3,765,90}; // Used to store people's ID codes
float balance[5] = {101.23,41,50,2,89}; //Used to store their current account balance
while (idFound != 1)
{
printf ("Please enter an account number to search: ");
scanf (" %d", &enteredID);
for (counter=0; counter<=5; ++counter) // Searches for the number the user entered
{
if (enteredID == id[counter])
{
idFound=1; // Tells program that it found the proper ID
break; // Breaks the loop early if the entered ID is found
}
else continue;
}
if (idFound) // Checks to see if the ID was found, then does stuff inside if
{
if ((balance[counter]) >= 50) //If the user's balance is over fifty it displays a message saying so
printf ("The current balance, %.2f, is over or equal to fifty. You're good to go!\n", balance[counter]);
else
printf ("The current balance, %.2f, is under fifty. You have some earning to do.\n", balance[counter]);
}
else
puts ("The ID you entered was not found. Please try again.");
}
return 0;
}
The problem is that ifFound- the variable used to determine if the User's ID was found- is always setting to 1. I can't figure out why. Any help would be appreciated. Also, I'm very new to this, so excuse me if its something very obvious.
#include <stdio.h>
int main ()
{
char idFound = 0;
int counter; //Used for sequential search for loop, and for getting balance from the second array, which is parallel
int enteredID; // The ID that the user entered, to be searched
int id[5] = {1,2,3,765,90}; // Used to store people's ID codes
float balance[5] = {101.23,41,50,2,89}; //Used to store their current account balance
while (idFound != 1)
{
printf ("Please enter an account number to search: ");
scanf (" %d", &enteredID);
for (counter=0; counter<=5; ++counter) // Searches for the number the user entered
{
if (enteredID == id[counter])
{
idFound=1; // Tells program that it found the proper ID
break; // Breaks the loop early if the entered ID is found
}
else continue;
}
if (idFound) // Checks to see if the ID was found, then does stuff inside if
{
if ((balance[counter]) >= 50) //If the user's balance is over fifty it displays a message saying so
printf ("The current balance, %.2f, is over or equal to fifty. You're good to go!\n", balance[counter]);
else
printf ("The current balance, %.2f, is under fifty. You have some earning to do.\n", balance[counter]);
}
else
puts ("The ID you entered was not found. Please try again.");
}
return 0;
}
The problem is that ifFound- the variable used to determine if the User's ID was found- is always setting to 1. I can't figure out why. Any help would be appreciated. Also, I'm very new to this, so excuse me if its something very obvious.