Reputation: 11
So, im a beginner in C programming and programming in general (i know a little javascript lol). Im trying to write a program that searches a integer array for a user's No. (number), log them in, and give them information about their account.
Note that im just making this for fun and practice, no particular reason.
My problem: I use a for
loop to increment my int i;
variable to search the array. The code:
main () {
int userNo [9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
float ultarCloud [9] = {12.04, 17.33, 8.9, 2.1, 13.75, 23.2, 25.0, 19.0, 5.34};
int scan;
int accountNo;
int found = 0;
int choice = 0
for (scan=0; scan<10; scan++)
{
printf("\n\t\tPlease enter your Ultra-Cloud account No.:");
scanf(" %d", &account);
if (accountNo == userNo[scan])
{
found = 1; //statement becomes true
break;
}
}
if (found = 1)
{
do {...}
what happens is the program only accepts what the value of int scan;
is when the user types their No. and will only end the for loop when a incorrect value is typed (something thats not in userNo array). and when an incorrect answer is entered it ends the program completely.
Am i doing something wrong? If you know what's wrong please tell me. Any advice is also greatly appreciated. I am using the code::blocks IDE btw. And just to let you know i havent been using stack overflow much so if im asking the "wrong" way then please let me know. thanks
Upvotes: 0
Views: 77
Reputation: 88155
You have a number of problems.
to search the array.
for (scan=0; scan<10; scan++)
{
printf("\n\t\tPlease enter your Ultra-Cloud account No.:");
scanf(" %d", &account);
if (accountNo == userNo[scan])
This is not how you search an array. You are asking the user for a new number for every spot the loop checks. To do a search you need to get one number to search for, and then search all spots for that number. As it is, the user could enter number 2 the first time around, when the loop is looking at the spot that contains 1, then the user could enter 1 while the loop is looking at the spot containing 2, and so on.
if (found = 1)
This is doing an assignment. Your compiler should be warning about this. If not, turn the warning level up. If it still doesn't, look for another compiler, or a lint tool that will catch this error. You can look at more warnings to fix here.
scanf(" %d", &account);
I assume that's a typo; Your program doesn't appear to contain a variable account
. It has an accountNo
.
for (scan=0; scan<10; scan++)
Your array has nine entries. This code will access userNo[9]
, which is out of bounds.
main ()
This is legal in C89, but you shouldn't do it. It's not legal in C99. Use int main()
. Also, C89 doesn't allow //
comments. Choose a language standard and stick with it. Personally I'd take C99 over C89.
Upvotes: 1
Reputation: 33
int found, choice;
found = choice = 0;
for (scan = 0; scan < 9; scan++)
{
printf("\n\t\tPlease enter your Ultra-Cloud account No.: ");
scanf(" %d", &accountNo);
if (accountNo == userNo[scan])
{
found = 1; //statement becomes true
break;
}
}
if (found == 1)
{
I also believe that your printf statement meant to read, printf("\n\t\tPlease enter your User Account No.: ");
Also, I recommend you watch this short video on style: http://cs50.tv/2012/fall/shorts/style/style-720p.mp4
Upvotes: 1
Reputation: 17880
You have a variable called account
in scanf
. I think you meant accountNo
You are searching in userNo
array and not in ultarCloud
array.
for (scan=0; scan<9; scan++)
{
printf("\n\t\tPlease enter your Ultra-Cloud account No.:");
scanf(" %d", &accountNo);
if (accountNo == ultarCloud[scan])
{
found = 1; //statement becomes true
break;
}
}
Upvotes: 0
Reputation: 5281
Change loop condition from
scan<10
to
scan<9
Your program is crashing from array index out of bound as your array ranges from:
userNo [0]
- userNo [8]
Upvotes: 0