School homework- a level computing
Design a data structure for the Natwest Bank.
It must hold the customers:
Name
Account Number
Balance
There will be 10 customers – Think back to your use of arrays. You must use an array [ ] of variables. Think about how you will access these.
here's my code.
#include<stdio.h>
int main(){
struct student
{
int balance;
int id;
char name[20];
};
struct student st1[9];
int i;
for(i=0;i<=9;i++){
printf("Enter the customers %d Name ID and balance", i+1);
scanf("%s""%d""%d", &st1[i].name, &st1[i].id, &st1[i].balance );
}
int k;
printf("what customer details do you want see");
scanf("%d", &k);
switch(k){
case 1:
printf("The customers 1 Name %d, ID %d and balance %d\n",st1[0].name, st1[0].id, st1[0].balance );
break;
case 2:
printf("The customers 2 Name %d, ID %d and balance %d\n",st1[1].name, st1[1].id, st1[1].balance );
break;
case 3:
printf("The customers 3 Name %d, ID %d and balance %d\n",st1[2].name, st1[2].id, st1[2].balance );
break;
case 4:
printf("The customers 4 Name %d, ID %d and balance %d\n",st1[3].name, st1[3].id, st1[3].balance );
break;
case 5:
printf("The customers 5 Name %d, ID %d and balance %d\n",st1[4].name, st1[4].id, st1[4].balance );
break;
case 6:
printf("The customers 6 Name %d, ID %d and balance %d\n",st1[5].name, st1[5].id, st1[5].balance );
break;
case 7:
printf("The customers 7 Name %d, ID %d and balance %d\n",st1[6].name, st1[6].id, st1[6].balance );
break;
case 8:
printf("The customers 8 Name %d, ID %d and balance %d\n",st1[7].name, st1[7].id, st1[7].balance );
break;
case 9:
printf("The customers 9 Name %d, ID %d and balance %d\n",st1[8].name, st1[8].id, st1[8].balance );
break;
case 10:
printf("The customers 10 Name %d, ID %s and balance %d\n",st1[9].name, st1[9].id, st1[9].balance );
break;
default :
printf("that's not a valid value");
break;
}
getchar();
getchar();
}
something wrong when the customers name come out.
something wrong when the customers name come out.
Dec 25, 2013 04:33 PM
(This post was last modified: Dec 25, 2013 04:33 PM by Sajuuk.)
Eww, put that damn code in code tags.
Code [code]Code Here[/ code]
i am new, so sorry.
how do you do that ?
but can you figure the problem out
DO NOT DEAL WITH THIS MEMBER
(Dec 25, 2013 04:07 PM)imrankhan Wrote: something wrong when the customers name come out.
something wrong when the customers name come out. Are we supposed to guess what the problem is? Is it doubling like the above statements?
problem is that when the user wants the information like id,name and balance, the name comes out in integers.Obviously the name is meant to come in string.
(Dec 25, 2013 04:41 PM)imrankhan Wrote: problem is that when the user wants the information like id,name and balance, the name comes out in integers.Obviously the name is meant to come in string.
You're using %d rather than %s when getting the name within your switch.
g4143 do you understand the promblem?
thank you
omg i was trying to avoid that problem
and how po i improve the code, any suggestions?
DO NOT DEAL WITH THIS MEMBER
Dec 25, 2013 05:18 PM
(This post was last modified: Dec 26, 2013 03:00 AM by Сlassy.)
(Dec 25, 2013 04:45 PM)imrankhan Wrote: g4143 do you understand the promblem?
thank you
omg i was trying to avoid that problem
and how po i improve the code, any suggestions?
Don't use that big ass switch statement.
Do this:
PHP Code
<?php int k; printf("what customer details do you want see"); scanf("%d", &k);
printf("The customers %d Name %s, ID %d and balance %d\n", k, st1[k-1].name, st1[k-1].id, st1[k-1].balance );
^ This can go out of bounds, but just throw it in an if statement and you're good to go.
Ex.
PHP Code
<?php int k; printf("what customer details do you want see"); scanf("%d", &k);
if(k >= 1 && k <= 10) printf("The customers %d Name %s, ID %d and balance %d\n", k, st1[k-1].name, st1[k-1].id, st1[k-1].balance ); else printf("Invalid input %d", k);
Dec 26, 2013 02:43 AM
(This post was last modified: Dec 26, 2013 02:44 AM by 0xDEAD10CC.)
(Dec 25, 2013 05:18 PM)Сlassy Wrote: Don't use that big ass switch statement.
Do this:
PHP Code
<?php int k; printf("what customer details do you want see"); scanf("%d", &k);
printf("The customers %d Name %s, ID %d and balance %d\n", k, st1[k].name, st1[k].id, st1[k].balance );
^ This can go out of bounds, but just throw it in an if statement and you're good to go.
Ex.
PHP Code
<?php int k; printf("what customer details do you want see"); scanf("%d", &k);
if(k >= 1 && <= 10) printf("The customers %d Name %s, ID %d and balance %d\n", k, st1[k].name, st1[k].id, st1[k].balance ); else printf("Invalid input %d", k);
Why are you using k for indexing, if you continue with logic that dictates a 1-based array exists? If you're using k directly to index a value in the array and NOT k - 1, then shouldn't that be k >= 0, and k < 10?
Still, something seems off about this:
Wouldn't you think? :) [hint]if(k >= 1 && <= 10)[/hint]
Code #include <stdio.h>
#define MAX_STUDENTS 10
typedef struct student
{
int balance;
int id;
char name[20];
} student;
int main()
{
student arr[MAX_STUDENTS];
int i, k;
for( i = 0; i < MAX_STUDENTS; i++ )
{
printf("Enter the customers %d Name ID and balance\n>>", i + 1);
printf("Ex: John 1 250\n");
scanf("%s %d %d", arr[i].name, &arr[i].id, &arr[i].balance);
}
getchar();
printf("What customer details do you want see? [1-%d]\n", MAX_STUDENTS);
if (!scanf("%d", &k) || (k > MAX_STUDENTS || k < 1))
{
printf("That is not a valid value...\n");
return 1;
}
int index = k - 1;
printf("The customers %d Name %s, ID %d and balance %d\n",
k, arr[index].name, arr[index].id, arr[index].balance);
getchar();
getchar();
}
I didn't include much handling for invalid input, this isn't my assignment. But here's how I would start going about it... Actually, personally I'd probably just ditch scanf() and go with fgets(). There's lots of functions for parsing data to a wanted datatype. I don't really program in C anyways.
DO NOT DEAL WITH THIS MEMBER
(Dec 26, 2013 02:43 AM)0xDEAD10CC Wrote: Why are you using k for indexing, if you continue with logic that dictates a 1-based array exists? If you're using k directly to index a value in the array and NOT k - 1, then shouldn't that be k >= 0, and k < 10?
Still, something seems off about this:
Wouldn't you think? :) [hint]if(k >= 1 && <= 10)[/hint]
Code #include <stdio.h>
#define MAX_STUDENTS 10
typedef struct student
{
int balance;
int id;
char name[20];
} student;
int main()
{
student arr[MAX_STUDENTS];
int i, k;
for( i = 0; i < MAX_STUDENTS; i++ )
{
printf("Enter the customers %d Name ID and balance\n>>", i + 1);
printf("Ex: John 1 250\n");
scanf("%s %d %d", arr[i].name, &arr[i].id, &arr[i].balance);
}
getchar();
printf("What customer details do you want see? [1-%d]\n", MAX_STUDENTS);
if (!scanf("%d", &k) || (k > MAX_STUDENTS || k < 1))
{
printf("That is not a valid value...\n");
return 1;
}
int index = k - 1;
printf("The customers %d Name %s, ID %d and balance %d\n",
k, arr[index].name, arr[index].id, arr[index].balance);
getchar();
getchar();
}
I didn't include much handling for invalid input, this isn't my assignment. But here's how I would start going about it... Actually, personally I'd probably just ditch scanf() and go with fgets(). There's lots of functions for parsing data to a wanted datatype. I don't really program in C anyways.
Ah thanks for pointing those out, I was tired and misread OP's switch cases. My post is now fixed and should be equivalent to OP's switch statement.
I will refrain from posting after 2 AM. Nothing good happens after 2 AM. HIMYM Reference
st1[9] should be st1[10] because there are 10 customers :)
(Dec 26, 2013 04:06 AM)hoer- Wrote: st1[9] should be st1[10] because there are 10 customers :)
Uhm, no? Because arrays start at the 0 position, so it should be st1[9].
(Dec 26, 2013 09:54 AM)Sajuuk Wrote: Uhm, no? Because arrays start at the 0 position, so it should be st1[9].
You must be confused, just because arrays start at 0 doesn't mean they should be defined like that. An array includes 0 as an element.
http://www.tutorialspoint.com/cprogramming/c_arrays.htm
st1[9] is correct considering he only needs 10 customers. If he were to do st1[10], that position would hold a random integer. So why have an extra element in the array if its not needed?
(Dec 26, 2013 11:07 AM)hoer- Wrote: You must be confused, just because arrays start at 0 doesn't mean they should be defined like that. An array includes 0 as an element.
http://www.tutorialspoint.com/cprogramming/c_arrays.htm
I'm confused? Huh, your post just proves my point. Look:
array[9] is array[0,1,2,3,4,5,6,7,8,9] <-- It's 10 elements, just what the OP needed.
|