![]() |
|
[C] Help.What's wrong. something wrong when the customers name come out. - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Help.What's wrong. something wrong when the customers name come out. (/showthread.php?tid=3951855) Pages: Pages
1
2
|
Help.What's wrong. something wrong when the customers name come out. - imrankhan - Dec 25, 2013 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. * - Sajuuk - Dec 25, 2013 Eww, put that damn code in code tags. Code [code]Code Here[/ code]* - imrankhan - Dec 25, 2013 i am new, so sorry. how do you do that ? but can you figure the problem out * - g4143 - Dec 25, 2013 (Dec 25, 2013 04:07 PM)imrankhan Wrote: something wrong when the customers name come out.Are we supposed to guess what the problem is? Is it doubling like the above statements? * - imrankhan - Dec 25, 2013 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. * - LevelUp - Dec 25, 2013 (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. * - imrankhan - Dec 25, 2013 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? * - Сlassy - Dec 25, 2013 (Dec 25, 2013 04:45 PM)imrankhan Wrote: g4143 do you understand the promblem? Don't use that big ass switch statement. Do this: PHP Code Ex. PHP Code * - 0xDEAD10CC - Dec 26, 2013 (Dec 25, 2013 05:18 PM)Сlassy Wrote: Don't use that big ass switch statement. 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: Code if(k >= 1 && <= 10)Wouldn't you think? :) [hint]if(k >= 1 && <= 10)[/hint] Code #include <stdio.h>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. * - Сlassy - Dec 26, 2013 (Dec 26, 2013 02:43 AM)TxTriton 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? 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 * - hoer- - Dec 26, 2013 st1[9] should be st1[10] because there are 10 customers :) * - Sajuuk - Dec 26, 2013 (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]. * - hoer- - Dec 26, 2013 (Dec 26, 2013 09:54 AM)Obsessed 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 * - InfectCode - Dec 26, 2013 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? * - Sajuuk - Dec 26, 2013 (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. 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. |