Strings Became Symbols When They Output in C [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a problem when i output some strings in my codes. When i input 1 string and then output it, there is no problem at all. But, when i input at least 2 strings, it went fail. The strings became symbols, except the last one. Here is the screenshot:
I've been using fflush(stdin) and fflush(stdout), but the problem is still exist.
So, what should i do? I need your advice.
Here is my complete codes:
#include <stdio.h>
#include <stdlib.h>
void menu();
void entry();
void search();
void PrintSingle();
void PrintComplete();
float TotalUsed(int i);
float RegularCost(int i);
float tax(int i);
float discount(int i);
float TotalPayment(int i);
#define NMaks 101
typedef enum {false=0,true=1} boolean;
typedef struct {int BillNumber,BillClass;float LastMeter,CurrentMeter;char name[];} BillDatabase;
BillDatabase bill[NMaks];
int DataAmount=0;
int main() {
menu();
return 0;
}
void menu() {
int i;
repeat:
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[1] Entries customer information\n");
printf("[2] Search customer\n");
printf("[3] Print single bill\n");
printf("[4] Print complete billing report\n");
printf("[5] Exit\n\n");
printf("Select the menu[1..5]: ");scanf("%d",&i);
switch (i) {
case 1 : entry();break;
case 2 : search();break;
case 3 : PrintSingle();break;
case 4 : PrintComplete();break;
case 5 : {
printf("\n\nGoodbye!");
getch();
break;
}
default : {
printf("\n\nWrong menu!");
goto repeat;
}
}
}
void entry() {
char repeat;
do {
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[1] Entries customer information\n\n");
DataAmount++;
printf("Bill number: ");scanf("%d",&bill[DataAmount].BillNumber);
printf("Customer name: ");fflush(stdin);fflush(stdout);gets(bill[DataAmount].name);
printf("Class[1..3]: ");scanf("%d",&bill[DataAmount].BillClass);
printf("Last meter: ");scanf("%f",&bill[DataAmount].LastMeter);
printf("Current meter: ");scanf("%f",&bill[DataAmount].CurrentMeter);
printf("\nEntry again[y/n]: ");repeat=getche();
} while (tolower(repeat)=='y');
menu();
}
void search() {
int i,BN;
boolean found;
char repeat;
do {
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[2] Search customer\n\n");
printf("Enter the bill number: ");scanf("%d",&BN);
found=false;
for (i=1;i<=DataAmount;i++)
if (BN==bill[i].BillNumber) {
found=true;
break;
}
if (found) {
printf("\nCustomer found!\n\n");
printf("Bill number\tN a m e Class\t\tLast meter\tCurrent meter\n");
fflush(stdin);fflush(stdout);
printf("%d\t\t%15.15s\t%d\t\t%.2f kWh\t%.2f kWh\n",bill[i].BillNumber,bill[i].name,bill[i].BillClass,bill[i].LastMeter,bill[i].CurrentMeter);
}
else
printf("\nCustomer not found!\n");
printf("\nSearch again[y/n]: ");repeat=getche();
} while (tolower(repeat)=='y');
menu();
}
void PrintSingle() {
int i,BN;
boolean found;
char repeat;
do {
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[3] Print single bill\n\n");
printf("Enter the bill number: ");scanf("%d",&BN);
found=false;
for (i=1;i<=DataAmount;i++)
if (BN==bill[i].BillNumber) {
found=true;
break;
}
if (found) {
printf("\nCustomer found!\n\n");
printf("Bill number\tN a m e Class\t\tLast meter\tCurrent meter\n");
fflush(stdin);fflush(stdout);
printf("%d\t\t%15.15s\t%d\t\t%.2f kWh\t%.2f kWh\n",bill[i].BillNumber,bill[i].name,bill[i].BillClass,bill[i].LastMeter,bill[i].CurrentMeter);
printf("Total used\tRegular cost\tTax\t\tDiscount\tTotal Payment\n");
printf("%.2f kWh\t$%7.2f\t$%7.2f\t$%7.2f\t$%7.2f\n",TotalUsed(i),RegularCost(i),tax(i),discount(i),TotalPayment(i));
}
else
printf("\nCustomer not found!\n");
printf("\nPrint again[y/n]: ");repeat=getche();
} while (tolower(repeat)=='y');
menu();
}
void PrintComplete() {
int i;
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[4] Print complete billing report\n\n");
printf("Bill number\tN a m e\t\tClass\tTotal Payment\n");
for (i=1;i<=DataAmount;i++) {
fflush(stdin);fflush(stdout);
printf("%d\t\t%15.15s\t\t%d\t$%.2f\n",bill[i].BillNumber,bill[i].name,bill[i].BillClass,TotalPayment(i));
}
printf("\nPress any key to the main menu");
getch();
menu();
}
float TotalUsed(int i) {
return bill[i].CurrentMeter-bill[i].LastMeter;
}
float RegularCost(int i) {
float price;
switch (bill[i].BillClass) {
case 1 : price=10.0;break;
case 2 : price=7.5;break;
default : price=13.75;
}
return TotalUsed(i)*price;
}
float tax(int i) {
float TaxPercentage;
switch (bill[i].BillClass) {
case 1 : TaxPercentage=1.5/100;break;
case 2 : TaxPercentage=0.25/100;break;
default : TaxPercentage=3.5/100;
}
return RegularCost(i)-(RegularCost(i)*TaxPercentage);
}
float discount(int i) {
float DiscountPercentage;
switch (bill[i].BillClass) {
case 1 : DiscountPercentage=3.0/100;break;
case 2 : DiscountPercentage=2.0/100;break;
default : DiscountPercentage=5.5/100;break;
}
if (RegularCost(i)>100.0)
return RegularCost(i)-(RegularCost(i)*DiscountPercentage);
else
return 0.0;
}
float TotalPayment(int i) {
return RegularCost(i)+tax(i)-discount(i);
}
Note: I use Code Blocks for the IDE.

I see a number of problems here.
First of all, your over-use of global variables is troubling. The array bill and the int DataAmount should definitely not be global. This is not good c programming practice and I would not trust someone whose code looks like this. Please give your "menu", "entry", "printcomplete", and "search" functions arguments so that you can make these variables local, not global.
Second, there are very few cases where it is acceptable to have many statements on the same line as in
printf("Customer name: ");fflush(stdin);fflush(stdout);gets(bill[DataAmount].name);
I'm getting a headache just looking at it!!! pleeease don't!
Third, You are not using array bounds correctly in this for loop, which is the cause of the bug you are asking about in the first place.
for (i=1;i<=DataAmount;i++) {
fflush(stdin);fflush(stdout);
printf("%d\t\t%15.15s\t\t%d\t$%.2f\n",bill[i].BillNumber,bill[i].name,bill[i].BillClass,TotalPayment(i));
}
Remember, in the C language, array indices start at 0 and end at "n - 1". Your loop is starting at 1 and ending at "n".
The reason for the bogus character is that you are accessing a BillDataBase that is outside the boundaries of the array bill.
Fourth, as Sami Kuhmonen said, you are not allocating any memory for your name array. Although this is probably not causing the bug you are seeing, it will almost certainly cause your program to crash with a segmentation fault at some point in the future. In order to fix this looming problem, you have 2 options
The easy way out: change your BillDatabase struct to the following
typedef struct
{
int BillNumber, BillClass;
float LastMeter, CurrentMeter;
char name[128];
} BillDatabase;
This is easy, but not good, because the name can never be more than 128 chars. Also, please don't EVER put the entire struct on one line. I almost started hitting myself over the head with my saxophone.
The harder way out: learn to use malloc(), and manually allocate the name arrays.

You are not allocating any memory for your names, so you're just writing into random places in memory. This is undefined behaviour.

Related

So many errors and not sure how to fix it

I'm currently still practicing my c programming skills but there are so many errors here that I'm confuse on what is wrong and how to fix it. It's for a database program that I was practicing on.
It keeps showing:
new2.c:86: error: request for member ‘previousreading’ in something not a structure or union
and
new2.c:94: error: ‘Break’ undeclared (first use in this function)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int custid;
char custname;
float currentreading;
float previousreading;
double charge;
int choice;
unsigned cust;
int revenue, meterdifference, BILL;
printf("----------------------------------\n");
printf("Electricity Management System\n");
printf("----------------------------------\n");
printf("\n1. Record Usage");
printf("\n2. Add Customer");
printf("\n3. Edit Customer");
printf("\n4. Delete Customer");
printf("\n5. Show Customer");
printf("\n6. Show Total monthly income");
printf("\n7. Exit");
scanf("%d",&choice);
if(choice >=1 || choice <=7)
{
switch(choice)
{
case 1: //Record Usage
printf("Enter Customer ID\n");
FILE *cfPtr;
if ((cfPtr = fopen("customer.txt", "r"))== NULL)
puts("This file could not be opened");
else
{
puts("Enter the customer ID, name.");
scanf("%d%29s", &cust.custid, cust.custname);
puts("Enter the current reading in kWh");
scanf("%d", cust.currentreading);
if(cust.currentreading < cust.previousreading)
puts("Input invalid");
else
{
if (cust.currentreading>=200)
{
cust.charge = (cust.currentreading - cust.previousreading)*21.80;
printf("\nThe charge is RM%f\n", &cust.charge);
}
else
{
if (cust.currentreading>=300)
{
cust.charge= ((cust.currentreading - cust.previousreading)*33.40)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
if (cust.currentreading>=600)
{
cust.charge= ((cust.currentreading - cust.previousreading)*51.60)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
if (currentreading>=900)
{
cust.charge = ((cust.currentreading - cust.previousreading)*54.60)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
cust.charge = ((cust.currentreading - cust.previousreading)*57.10)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
}
}
}
}
}
Break;
case2: //Add Customer
puts("This option allows user to add new customer");
printf("Enter Customer ID and name.");
scanf("%d%c", &cust.custid, cust.custname);
puts("To return to menu");
Break;
case 3: //Edit Customer
puts( "This option allows user to edit customer info");
Break;
case 4: //delete customer
puts( "This option allows user to delete customer");
Break;
case 5: //Show Customer
printf("To show customer information\n");
FILE*tPtr;
char custid[100],custname[100];
int previousreading,currentreading;
double charge;
printf("\n Show Customer\n");
if((tPtr= fopen("customer.txt","r"))==NULL){
puts("File not found");
}
else{
printf("%-15s%-25s%-20s%-15s%-15s\n","ID","Name","Previous Reading","Current Reading","Charges");
while(!feof(tPtr)){
fscanf(tPtr,"%[^;];%[^;];%d;%d;%lf",cust.custid,cust.custname,&cust.previousreading,&cust.currentreading,&cust.charge);
printf("%s\t\t%-25s%-20d%-15d%-15.2lf",cust.custid,cust.custname,cust.previousreading,cust.currentreading,cust.charge);
}
fclose(tPtr);
}
printf("\n\n");
Break;
case 6: //Show total income(monthly)
puts("To show monthyly income");
printf("total usagekWh, meterdifference");
printf("%-15s%-35.2d\n", "Total UsagekWh","meterdifference");
scanf("%-16dtotal usage(kWh)%-24d: %.2f",&meterdifference);
printf("%-13dtotal revenue%-24d: %.2f",BILL);
revenue=BILL;
printf("revenue is %.2f", BILL);
Break;
case 7: //Exit
Break;
}
}
else
printf("\nError. Number not in choices.");
return 0;
}
typedef struct{
int custid[50];
char custname[100];
int previousreading;
int currentreading;
float charges;
}cust;
Put the typedef before main. typedefs must occure before you use them just as vaiables.
Replace unsigned cust; by cust cust;. unsigned cust; is the same as unsigned int cust; and declares an unsigned integer, you want to declare a cust.
Replace float charges; by float charge; in the typedef
Replace Break; by break;. Case matters in C. Break is not Break, just as Int is not int.
Then it compiles.
Now if it it runs correctly or not is another story.
There is not a single structure in your code, not in the form of a variable declaration nor as a type definition1, and you are treating cust which is simply an unsigned int as if it was a structure, perhaps you mean
struct {
float previousreading;
float currentreading;
/* And so on */
} cust;
Also, there is no Break keyword in c, it's break, all lower case.
But,
Don't do it, create a new struct so that you can use declare variables of type struct Costumer for example. Like at the end of your code, except that the compiler needs to know about it before using it, and the cust variable should have it's type.
A char is not a string type, if you want a string you need an array of char, so char custname; is not going to work for the name string.
Use meaningful names for your variables, and the members if your structure and the type name too. Like costumer instead of cust.
Additional NOTE
See Why while (!foef(file)) is always wrong. Your code will always attempt a read with fscanf() that will fail but it proceeds to print the data, it's very likely that your last row is printed twice once you make the code compile.
Instead, check the return value of fscanf(), if you don't know what it returns and don't fully understand it you can always read fscanf(3) documentation.
1At least not before you attempt to use it.

Why does this give junk value?

I am getting garbage / junk values as output when my program is run and the data displayed.
Why is it so?
Can someone help me to understand how to properly pass by pointers and not get junk values?
This program is about stack creation of struct books type variables.
By default shouldn't the variable bks pass by pointer and change when b is changed?
bks is still storing garbage value.
Here is my code:
#include<stdio.h>
#include<stdlib.h>
struct books
{
int yrpub;
char name[100],author[50];
};
int top=-1;
int push(struct books b[],int top,int n)
{
if(top==n-1)
return -1;
else
{
++(top);
printf("Enter books info: \n");
printf("Enter name: ");
gets(b[top].name);
printf("Enter author: ");
gets(b[top].author);
printf("Enter Year of publish: ");
scanf("%d",&b[top].yrpub);
return top;
}
}
void display(struct books b[],int top)
{
int i;
if(top==-1)
printf("No books in the stack...");
for(i=0;i<=top;i++)
{
printf("Details of book %d: \n",i+1);
printf("Name: %s\nAuthor: %s\nYear of publish: %d\n",b[i].name,b[i].author,b[i].yrpub);
}
system("pause");
}
int main()
{
struct books bks[10];
int ch;
system("cls");
printf("Select an option:\n");
printf("1. Push book\n2. Pop book\n3. Peep book\n4. Display all books info\n5. Exit\n");
printf("Enter a choice: ");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:
system("cls");
top=push(bks,top,10);
break;
case 4:
system("cls");
display(bks,top);
break;
case 5: exit(0);
default: printf("\nWrong choice...Please retry.");
long i,j;
for(i=0;i<1000000;i++)
for(j=0;j<100;j++);
}
main();
}
Each time you recursively call main(), you create a new array bk.
The information you entered in the previous invocation of main() is hidden from the new one.
To iterate is human; to recurse, divine.
In this context, give up divinity for humanity. Use iteration — in this context it is better.
This is your primary problem; there may also be other off-by-one or other errors.
push:
if(top==n-1)
return -1;
main:
top=push(bks,top,10);
top is reset when the stack is full
Edit:
And the second problem is main being called again, struct books bks[10] is reset in the next main, it is a recursion. Declare bks as global or go with a while loop instead of recursion.
while (1) {
getChoices();
if (exit)
/* exit from here */
process();
}

When I run this program, after entering a name, I get a segmentation fault. How can I fix this to make it work properly?

Restauraunt.c
This program allows you to create a restaurant menu, stores it in a file, and then rates each item on the menu. It uses file functions to output everything into a file that can then be viewed through almost any program. When the program gets to the line in nametofile() 'fprintf(restauraunt, "%s Restauraunt\n\n",name);' the program gives a segmentation fault. I do not know why it is doing this, I have attempted several different methods of debugging, but none have worked. If you have any suggestions, please comment them below.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
FILE *restauraunt;
char name[20];
char item[20];
char price[20];
int count=0;
void nametofile();
void rate();
void itemtofile();
void counter();
void renamefile();
int main()
{
int i,j;
int num;
printf("Restauraunt Creator\n\n");
printf("Enter the name of your restauraunt:\n");
scanf("%s",&name);
nametofile();
printf("\nEnter the number of items to be included in your menu:\n");
scanf("%d", &num);
/* Cycles through each entry to the menu */
for(i=0;i<num;i++)
{
counter();
fpurge(stdin);
printf("\nPlease enter the name of item number %d:\n",count);
scanf("%s", &item);
printf("\nPlease enter the price of item number %d:\n",count);
scanf("%s", &price);
itemtofile();
rate();
}
renamefile();
}
/*void nametofile()
{
restauraunt = fopen("restauraunt","w");
fprintf(restauraunt, "%s Restauraunt\n\n",name);
fclose(restauraunt);
}*/
/* The function that sends the restaurant name to the file */
void nametofile()
{
int i;
i = strlen(name);
name[i+1] = '\0';
restauraunt = fopen("restauraunt","w");
/* the line that gives a segmentation fault */
fprintf(restauraunt, "%s Restauraunt\n\n",name);
fclose(restauraunt);
}
/* rates each menu item */
void rate()
{
int rating;
srandom((unsigned)time(NULL));
restauraunt = fopen("restauraunt", "a");
rating = random() % 5 + 1;
fprintf(restauraunt,"Your food's rating was:\t%d stars!",rating);
switch(rating)
{
case 1:
{
fprintf(restauraunt," Here's why: Your food was not very good tasting and the price was ridiculously high.\n");
break;
}
case 2:
{
fprintf(restauraunt," Here's why: Your food was mildly good tasting and the price was too high.\n");
break;
}
case 3:
{
fprintf(restauraunt," Here's why: Your food was somewhat good tasting and the price was fair.\n");
break;
}
case 4:
{
fprintf(restauraunt," Here's why: Your food was quite good tasting and the price was very nice.\n");
break;
}
case 5:
{
fprintf(restauraunt," Here's why: Your food was very delicious and the price was amazingly low.\n");
break;
}
}
}
/* sends each item to the file */
void itemtofile()
{
restauraunt = fopen("restauraunt","a");
fprintf(restauraunt, "%s: $%s\nRating:",item,price);
fclose(restauraunt);
}
/* counts up one each time function is called */
void counter()
{
count += 1;
}
/* renames the file at the end */
void renamefile()
{
int x,y;
char bridge[] = { "menu" };
name[0] = tolower(name[0]);
x = strcat(name,bridge);
y = rename("restauraunt",name);
}
name is a char array. When you pass it to scanf or other functions, it decays to a pointer, so you do not need the & operator:
scanf("%19s", name);
When you read strings with scanf, it is a good idea to pass the size limit: this lets you avoid buffer overruns. Since name is declared as char[20], you pass 19, because one more char needs to be reserved for the null terminator.

How to approach and optimize code in C

I am new to C and very much interested in knowing how to approach any problem which has more than 3 or 4 functions, I always look at the output required and manipulate my code calling functions inside other functions and getting the required output.
Below is my logic for finding a students record through his Id first & then Username.
This code according to my professor has an excessive logic and is lacking in many ways, if someone could assist me in how should I approach any problem in C or in any other language it would be of great help for me as a beginner and yes I do write pseudo code first.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int id; //Assuming student id to be unique
int age;
char *userName; //Assuming student userName to be unique
char *dept;
}student; // Alias "student" created for struct
student* createstruct(); // All function prototype declared
student* createArray();
void addstruct(student* s2);
void searchChar(student* s2,int num);
void searchInt(student* s2,int num);
student* createstruct() // function createStruct() to malloc data of struct student.
{
student *s;
s = (student*)malloc(sizeof(student));
s->userName = (char*)malloc(sizeof(char)*32);
s->dept = (char*)malloc(sizeof(char)*32);
printf("please enter id ");
scanf("%d",&s->id);
printf("please enter age ");
scanf("%d",&s->age);
printf("please enter userName ");
scanf("%31s",s->userName);
printf("please enter department ");
scanf("%31s",s->dept);
printf("\n");
return s;
}
student* createArray()
{
student *arr; //declaration of arr poiter, type struct student
arr = (student*)malloc(sizeof(student)*10); // memory allocated for a size of 10
return arr;
}
void addstruct(student *s2) // function for adding data to the structures in array
{
int i,num;
student* s1;
printf("please enter the number of records to add:");
scanf("%d",&num);
printf("\n");
if(num>0 && num<11)
{
for(i=0;i<num;i++) // if user want to enter 5 records loop will only run 5 times
{
s1 = createstruct();
s2[i].id = s1->id; // traversing each element of array and filling in struct data
s2[i].age = s1->age;
s2[i].userName = s1->userName;
s2[i].dept= s1->dept;
}
}
else if(num>10) // if user enters more than 10
{
for(i=0;i<10;i++) // loop will still run only 10 times
{
s1 = createstruct();
s2[i].id = s1->id;
s2[i].age = s1->age;
s2[i].userName = s1->userName;
s2[i].dept = s1->dept;
}
printf("Array is full"); // Array is full after taking 10 records
printf("\n");
}
searchInt(s2,num); // Calling searchInt() function to search for an integer in records
searchChar(s2,num); // Calling searchChar() function to search for a string in records
free(s1);
free(s2);
}
void searchChar(student* s2,int num) // function for searching a string in records of structure
{
char *c;
int i;
c = (char*)malloc(sizeof(char)*32);
printf("please enter userName to search ");
scanf("%31s",c);
printf("\n");
for (i=0;i<num;i++) //num is the number of struct records entered by user
{
if ((strcmp(s2[i].userName,c)==0)) //using strcmp for comparing strings
{
printf("struct variables are %d, %d, %s, %s\n", s2[i].id,s2[i].age,s2[i].userName,s2[i].dept);
break;
}
else if(i == num-1)
{
printf("nothing in userName matches: <%s>\n",c);
break;
}
}
}
void searchInt(student* s2,int num) //searchs for an integer and prints the entire structure
{
int i,z;
printf("please enter id to search ");
scanf("%d",&z);
printf("\n");
for (i=0;i<num;i++)
{
if (s2[i].id == z)
{
printf("struct variables are %d, %d, %s, %s\n\n", s2[i].id,s2[i].age,s2[i].userName,s2[i].dept);
break;
}
else if(i == num-1)
{
printf("nothing in id matches: <%d>\n\n",z);
break;
}
}
}
int main(void)
{
student *s2;
s2 = createArray();
addstruct(s2);
return 0;
}
I'm not going to go into optimizing, because if you wanted better theoretical performance you would probably go with different data structures, such as ordered arrays/lists, trees, hash tables or some kind of indexing... None of that is relevant in this case, because you have a simple program dealing with a small amount of data.
But I am going to tell you about the "excessive logic" your professor mentioned, taking your searchInt function as an example:
for (i=0;i<num;i++)
{
if (s2[i].id == z)
{
printf("struct variables are %d, %d, %s, %s\n\n", s2[i].id,s2[i].age,s2[i].userName,s2[i].dept);
break;
}
else if(i == num-1)
{
printf("nothing in id matches: <%d>\n\n",z);
break;
}
}
The thing here is that every time around the loop you're testing to see if you're at the last element in the loop. But the loop already does that. So you're doing it twice, and to make it worse, you're doing a subtraction (which may or may not be optimized into a register by the compiler).
What you would normally do is something like this:
int i;
student *s = NULL;
for( i = 0; i < num; i++ )
{
if( s2[i].id == z ) {
s = &s2[i];
break;
}
}
if( s != NULL ) {
printf( "struct variables are %d, %d, %s, %s\n\n",
s->id, s->age, s->userName, s->dept );
} else {
printf("nothing in id matches: <%d>\n\n",z);
}
See that you only need to have some way of knowing that the loop found something. You wait for the loop to finish before you test whether it found something.
In this case I used a pointer to indicate success, because I could then use the pointer to access the relevant record without having to index back into the array and clutter the code. You won't always use pointers.
Sometimes you set a flag, sometimes you store the array index, sometimes you just return from the function (and if the loop falls through you know it didn't find anything).
Programming is about making sensible choices for the problem you are solving. Only optimize when you need to, don't over-complicate a problem, and always try to write code that is easy to read/understand.

Programming in C, integer distribution error, cannot find mistake

I'm writing a code for a simple text based game, and when I purchase a car, the HP and TOPSPEED are registering for integers different than specified.
I've looked over the code myself, perhaps I'm not seeing it, but the Dodge Intrepid should register at 214hp and 140mph top speed, however when I enter the race menu, it registers for 320hp and 160mph top speed, which are the settings of the Mitsubishi 3000GT and the Dodge Stealth of the last beta. I imported the "race" code from the previous beta, being careful to omit any information about the cars used in the previous beta. If you can find my mistake or point anything out, it would be greatly appreciated (I'm including the code with my post). Thanks for your time. (I'm probably overlooking something) (Written in C, compiled and linked with DevC++)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*HP at RACE is malfunctioning, look over code.*/
int rnd(int range);
void seedrnd(void);
int main()
{
char name[15];
char ocar;
char exit;
char partsA;
char partsB;
char partsC;
char choice;
char dealer;
char model;
int bhp;
int ohp;
int tspd;
int otspd;
int score;
int oscore;
int cash;
int bonus;
int parts1;
int parts2;
int parts3;
int car1;
int car2;
int car3;
int car;
int hp1;
int hp2;
int hp3;
int tspd1;
int tspd2;
int tspd3;
printf("What is your name?\n");
scanf("%s",name);
parts1=1;
parts2=1;
parts3=1;
hp1=214;
tspd1=140;
hp2=490;
tspd2=190;
hp3=320;
tspd3=160;
car=0;
car1=0;
car2=0;
car3=0;
cash=40000;
while(exit!='-')
{
printf("\nMenu:\n1.Garage\n2.Race\n3.Parts Shop\n4.Dealerships\n\n");
choice=getch();
if(choice=='1')
{
printf("Welcome to %s's garage:\n\n",name);
if(car1>0)
{
printf("1.Dodge Intrepid\n");
}
if(car2>0)
{
printf("2.1988 Vector M12\n");
}
if(car3>0)
{
printf("3.Mitsubishi 3000GT\n");
}
car=getch();
if(car=='1')
{
if(car1>0)
{
printf("You are now driving a Dodge Intrepid\n");
car=1;
}
if(car1<1)
{
printf("You do not yet own this vehicle\n");
}
}
if(car=='2')
{
if(car2>0)
{
printf("You are now driving a 1988 Vector M12\n");
car=2;
}
if(car2<1)
{
printf("You do not yet own this vehicle\n");
}
}
if(car=='3')
{
if(car3>0)
{
printf("You are now driving a Mitsubishi 3000GT\n");
car=3;
}
if(car<1)
{
printf("You do not yet own this vehicle\n");
}
}
}
if(choice=='2')
{
if(car=1)
{
bhp=hp1;
tspd=tspd1;
}
if(car=2)
{
bhp=hp2;
tspd=tspd2;
}
if(car=3)
{
bhp=hp3;
tspd=tspd3;
}
/*Begin Race Mechanism*/
printf("Your selected car has %dhp and %dmph top speed\n\n",bhp,tspd);
printf("Now choose your opponent:\n");
printf("1.Rachel (150-250hp 110-130mph)\n");
printf("2.Kyle (250-350hp 130-170mph)\n");
printf("3.Darrian (350-450hp 170-210mph)\n");
printf("4.Chelsea (450-550hp 210-220mph)\n");
ocar=getch();
seedrnd();
if(ocar=='1')
{
ohp=rnd(100)+151;
otspd=rnd(20)+111;
bonus=500;
}
else if(ocar=='2')
{
ohp=rnd(100)+251;
otspd=rnd(40)+131;
bonus=1000;
}
else if(ocar=='3')
{
ohp=rnd(100)+351;
otspd=rnd(40)+171;
bonus=1500;
}
else if(ocar=='4')
{
ohp=rnd(100)+451;
otspd=rnd(10)+211;
bonus=2000;
}
else
{
printf("Haha, you're racing Eli\nNo contest here\n");
ohp=2;
otspd=25;
}
printf("Match-up:%s %dhp %dmph top speed\n",name,bhp,tspd);
printf(" vs \n");
printf("Opponent: %dhp %dmph top speed\n",ohp,otspd);
getch();
score=bhp*tspd;
oscore=ohp*otspd;
printf("Let the race begin\n");
sleep(1000);
printf("3\n");
sleep(1000);
printf("2\n");
sleep(1000);
printf("1\n");
sleep(1000);
printf("!\n\n");
sleep(2000);
printf("The race is over, and the winner is!!!\n\n\n\n");
sleep(3000);
if(score>oscore)
{
printf("YOU!!!\n");
cash=cash+(bonus);
}
else if(score<oscore)
{
printf("Your Opponent...\n");
}
else
{
printf("...neither of you, it was a tie!\n");
cash=cash+(bonus/2);
}
printf("You now have$%d.\n\n",cash);
}/* Closes choice 2*/
if(choice=='3')
{
/* Parts Shop*/
printf("Under Construction\n");
}
if(choice=='4')
{
printf("Press 'y' to exit\n");
printf("Please Select a Dealership\n");
printf("1.Dodge\n2.Vector\n3.Mitsubishi\n\n");
while(dealer!='y')
{
dealer=getch();
if(dealer=='1')
{
printf("DODGE:\n");
if(car1<1)
printf("1.Dodge Intrepid (214hp 140mph) $21,000\n\n");
model=getch();
if(model=='1')
{
if(cash<21000)
{
printf("You cannot afford this vehicle\n\n");
}
if(cash>=21000)
{
if(car1>0)
{
printf("You have already purchased this vehicle\n\n");
}
if(car1<1)
{
car1=car1+1;
cash=cash-21000;
printf("Thank You for purchasing this Dodge Intrepid\n\n");
}
}
}
}
if(dealer=='2')
{
printf("VECTOR:\n");
if(car2<1)
printf("1.1988 Vector M12 (490hp 190mph)$180,000\n\n");
model=getch();
if(model=='1')
{
if(cash<180000)
{
printf("You cannot afford this vehicle\n\n");
}
if(cash>=180000)
{
if(car2>0)
{
printf("You have already purchased this vehicle\n\n");
}
if(car2<1)
{
car2=car2+1;
cash=cash-180000;
printf("Thank You for purchasing this 1988 Vector M12\n\n");
}
}
}
}
if(dealer=='3')
{
printf("MITSUBISHI:\n");
if(car3<1)
printf("1.Mitsubishi 3000GT (320hp 160mph) $60,000\n\n");
model=getch();
if(model=='1')
{
if(cash<60000)
{
printf("You cannot afford this vehicle\n\n");
}
if(cash>=60000)
{
if(car3>0)
{
printf("You have already purchased this vehicle\n\n");
}
if(car3<1)
{
car3=car3+1;
cash=cash-60000;
printf("Thank You for purchasing this Mitsubishi 3000GT\n\n");
}
}
}
}
}
}
exit=getch();
}
return(0);
}
int rnd(int range)
{
int i;
i=rand()%range;
return(i);
}
void seedrnd(void)
{
srand((unsigned)time(NULL));
}
if(car=1)
{
bhp=hp1;
tspd=tspd1;
}
You probably mean if( car == 1 ) { ... this just sets car to 1, and "returns" 1 to the if. Then you do the same for car == 2 and car == 3.
= is the Assignment operator, while == is the comparison operator! The first assigns a value to a variable, while the latter returns true if the are the same (note, for char* you need strcmp()).
Generally you should break your program into small functions and also check for the unhappy case (e.g. what happens if user inputs something not acceptable?)
Another thing that you should pay some attention to is this
scanf("%s",name);
what if someone types in more than 15 characters? You will start righting in memory you do not own and this invokes Undefined Behaviour. One easy trick for that is to do
scanf("%14s", name);
This will restrict the input to 14 characters (and will keep the 15th for the nul terminator). Read more here: http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
As others have noted in the comments, consider using structs to make your life easier. I haven't read the program in too detail to understand the exact structure of your game, but you could have for example a struct for the user:
struct player
{
char name[15];
car* owned_cars; //Linked List!
}
Where car is defined like that:
struct car
{
int max_speed;
int colour; //or even better an enum here
int seats;
//... etc.
car* next_car; //Make the Linked List's next node here!
}
Also! If you make a LinkedList, one mistake that usually happens is that they forgot to initialize thieir pointers to NULL. So :
car* new_car = malloc( sizeof(car) );
new_car->next_car = NULL;
If you don't do that, next_car will contain a random/garbage number! So when you try to go through the List
while(new_car->next_car != NULL)
{
//....
}
You will access memory you do not own.
Good luck and have fun!
There's also an issue in the following code:
if(car=='3')
{
if(car3>0)
{
printf("You are now driving a Mitsubishi 3000GT\n");
car=3;
}
if(car<1) // SHOULD BE car3<1
{
printf("You do not yet own this vehicle\n");
}
}
The integer distribution is wrong, because
int rnd(int range)
{
int i;
i=rand()%range;
return(i);
}
is wrong. That ought to be
int rnd(int range)
{
return rand() / ( RAND_MAX / range + 1 );
}
Read more here: Eternally Confuzzled: using rand()
Edit to the comment:
With this, I specifically respond to your question integer distribution error: the random distribution will not be uniform. Meaning, some numbers will appear significantly less frequently than others, not the property of a true (pseudo) random sequence.
The linked article contains more elaborate explanation of precisely what happens.
You are using the rnd() function defined at the bottom of in quite some places, and to be honest I haven't gone through all the code to understand the implications on what the program does, so I don't know whether it also answers some of the other questions you (vaguely) desscribe in the OP.
This section:
if(car=1)
{
bhp=hp1;
tspd=tspd1;
}
if(car=2)
{
bhp=hp2;
tspd=tspd2;
}
if(car=3)
{
bhp=hp3;
tspd=tspd3;
}
You are assigning car to 1, then 2, then 3: Use == instead of =.

Resources