-10

Well im Trying to finish up some coding ..... and im not sure what exactly is wrong with my program The error seems to be in the output area as i think its giving me the memory locations of the variables but i initialized to 0 and im not sure what might be wrong now ...Confused face. Here is the program though ( Just the error area, Not full program ).

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct Mica
{
    char FleNme [256];
}Mscl;

typedef struct Blade
{
    char BladeName [80];
}blade;

typedef struct eader
{
    char Header[256];
}header;

int main()
{
    Mscl Access;
    blade info;
    int hcount =0;
    int x=0;
    char decision;
    float BladePrice = 0;
    int spin = 0;
    int speed= 0;
    int weight= 0;
    header inf[hcount];
    FILE *Nmes;

    system("cls");

    printf("Please enter the name of the file you wish to Add:\n ");
    scanf("%s",&Access.FleNme);

    if((Nmes=fopen(Access.FleNme,"r"))==NULL)
    {
        printf("File can be created\n");
        Nmes=fopen(Access.FleNme,"w");
    }
    else
    {
        printf("File exist Already redirecting to Menu y for yes n for no: ");
        scanf("%s",&decision);

        if(decision=='y')
        {
            main();
        }
        else
        {
            exit(0);
        }
    }

    printf("Format Example: Bladename  BladePrice  Spin  Control  Weight\n");
    printf("Please Input how many headers you will be using: ");
    scanf("%d",&hcount);

    for (x=0;x<hcount;x++)
    {
        printf("Please enter The Header Name: ");
        scanf("%s",&inf[x].Header);
    }

    system("cls");
    printf("Please enter Name of Item: ");
    scanf("%s",&info.BladeName);
    printf("\nPlease enter Price of Item: ");
    scanf("%f",&BladePrice);
    printf("\nPlease enter Speed of Item: ");
    scanf("%d",&speed);
    printf("\nPlease enter Spin of Item: ");
    scanf("%d",&spin);
    printf("\nPlease enter weight of Item: ");
    scanf("%d",&weight);

    for(x=0;x<hcount;x++)
    {
        printf("%s\t",inf[x].Header);
    }

    printf("\n%s\t%.2f\t%d\t%d\t%d",&info.BladeName,&BladePrice,&speed,&spin,&weight);
}
Sergei Danielian
  • 4,938
  • 4
  • 36
  • 58
  • 2
    What is the error? What output did you get, and what did you expect? – Oliver Charlesworth Jan 31 '13 at 14:13
  • 1
    Perhaps the compiler is being obstinate because of the poor formatting. – William Pursell Jan 31 '13 at 14:17
  • @OliCharlesworth The error is within the .... lines after system("cls").... expected what the user input as output but instead it ignores it and .... i think it just gives me the memory location – Akerr Stephen Jan 31 '13 at 14:19
  • If `fopen` fails, it does not necessarily mean that the file does not exist, nor that it can be created. – William Pursell Jan 31 '13 at 14:19
  • @WilliamPursell not sure im getting you – Akerr Stephen Jan 31 '13 at 14:22
  • He means that getting `NULL` from a `fopen()` call does not guaranty at all that the given file does not exist. Something might have happened that prevented fopen from accessing the file hence the NULL value. Moreover, it's a pure waste of time to test a file existence by opening it ... Just take a look here : http://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform – Rerito Jan 31 '13 at 14:25

1 Answers1

2

One error is

char decision;
...
scanf("%s",&decision);

You're asking scanf to write a char array to a single char. This will have undefined results but may well result in stack variables following decision being over-written.

To fix this, either use %c as your format specifier

scanf("%c",&decision);

or declare decision as a char array

char decision[32];
scanf("%.31s",decision);

If you still see further problems, please be much more specific about where in your program they occur. It would also be a big help if you took the time to format your code, fixing its indentation.

simonc
  • 41,632
  • 12
  • 85
  • 103
  • Well yea its suppose to be overwritten initially and yes i will change to c though *facepalm* didnt realize that ...thanks though but the big error lies after system("cls") – Akerr Stephen Jan 31 '13 at 14:20