Unwanted character being added to string in C
Posted
by
Church
on Stack Overflow
See other posts from Stack Overflow
or by Church
Published on 2012-10-15T22:57:57Z
Indexed on
2012/10/15
23:01 UTC
Read the original article
Hit count: 181
I have a program that gives you shipping addresses from an input file. However at the beginning of one of the strings, order.add_one, a number is being added to the beginning of the string, that number is equivalent to the variable "choice" every time. Why is it doing this?
#include <stdio.h>
#include <math.h>
#include <string.h>
//structure
typedef struct
{char cust_name[25];
char cust_id[3];
char add_one[30];
char add_two[30];
char bike;
char risky;
int number_ordered;
char cust_information[500];
}ORDER;
ORDER order;
int main(void){
fflush(stdin);
system ( "clear" );
//initialize variables
float price;
float m = 359.95;
float s = 279.95;
//while loop, runs until user declares they no longer wish to input orders
while (1==1){
printf("Options: \nEnter Customer information manually : 1 \nSearch Customer by ID(input.txt reader) : 2 \n");
int option = 0;
scanf(" %d", &option);
if (option == 1){
//Print and scan statements
printf("Enter Customer Information\n");
printf("Customer Name: ");
scanf(" %[^\n]s", &order.cust_name);
printf("\nEnter Address Line One: ");
scanf(" %[^\n]s", &order.add_one);
printf("\nEnter Addres Line Two: ");
scanf(" %[^\n]s", &order.add_two);
printf("\nHow Many Bicycles Are Ordered: ");
scanf(" %d", &order.number_ordered);
printf("\nWhat Type Of Bike Is Ordered\n M Mountain Bike \n S Street Bike");
printf("\nChoose One (M or S): ");
scanf(" %c", &order.bike);
printf("\nIs The Customer Risky (Y/N): ");
scanf(" %c", &order.risky);
system ( "clear" );
}
if (option == 2){
FILE *fpt;
fpt = fopen("input.txt", "r");
if (fpt==NULL){
printf("Text file did not open\n");
return 1;
}
printf("Enter Customer ID: ");
scanf("%s", &order.cust_id);
char choice;
choice = order.cust_id[0];
char x[3];
int w, u, y, z;
char a[10], b[10], c[10], d[10], e[20], f[10], g[10], i[1], j[1];
int h;
printf("%s value of c", c);
if (choice >='1'){
while ((w = fgetc(fpt)) != '\n' ){
}
}
if (choice >='2'){
while ((u = fgetc(fpt)) != '\n' ){
}
}
if (choice >='3'){
while ((y = fgetc(fpt)) != '\n' ){
}
}
if (choice >= '4'){
while ((z = fgetc(fpt)) != '\n' ){
}
}
printf("\n");
fscanf(fpt, "%s", x);
fscanf(fpt, "%s", a);
printf("%s", a);
strcat(order.cust_name, a);
fscanf(fpt, " %s", b);
printf(" %s", b);
strcat(order.cust_name, " ");
strcat(order.cust_name, b);
fscanf(fpt, "%s", c);
printf(" %s", c);
strcat(order.add_one, "\0");
strcat(order.add_one, c);
fscanf(fpt, "%s", d);
printf(" %s", d);
strcat(order.add_one, " ");
strcat(order.add_one, d);
fscanf(fpt, "%s", e);
printf(" %s", e);
strcat(order.add_two, e);
fscanf(fpt, "%s", f);
printf(" %s", f);
strcat(order.add_two, " ");
strcat(order.add_two, f);
fscanf(fpt, "%s", g);
printf(" %s", g);
strcat(order.add_two, " ");
strcat(order.add_two, g);
strcat(order.add_two, "\0");
fscanf(fpt, "%d", &h);
printf(" %d", h);
order.number_ordered = h;
fscanf(fpt, "%s", i);
printf(" %s", i);
order.bike = i[0];
fscanf(fpt, "%s", j);
printf(" %s", j);
order.risky = j[0];
fclose(fpt);
printf("%s %s %s %d %c %c", order.cust_name, order.add_one, order.add_two, order.number_ordered, order.bike, order.risky);
}
© Stack Overflow or respective owner