Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6929743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:21:24+00:00 2026-05-27T11:21:24+00:00

Im doing C homework and trying to get this program to run, but I

  • 0

Im doing C homework and trying to get this program to run, but I get this:

http://i.imgur.com/UUdLB.png

Can anyone please run this and see if you get the same? (Change the output directory for the text files)

I’ve been trying for a long time and just cannot do it:

Here’s the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

/////////////////
#define SIZE 2
/////////////////

struct Stock
{
    char name[10];
    int numShares;
    float buyShare,currPrice,fees;
    float initCost,currCost,profit;
};

/* Load the data from the keyboard and calculates 
the Initial Cost, Current Cost, and Profit 
for each stock via an array */

void load(struct Stock s[], int n) 
{
    char nameCompare[30] = "Open the bay doors";
    int i;
    for(i=0;i<n;i++)
    {
        printf("Enter the Stock Name\n");
        printf(">");
        gets(s[i].name);

        /////////////////////////////////////
        if(strncmp (s[i].name,nameCompare,10) == 0)
        {
            printf("\tI'm sorry, Dave, I'm afraid I can't do that\n");
            Sleep(3000);
            exit(1);
        }

        /////////////////////////////////////
        printf("Enter the Number of Shares\n");
        printf(">");
        scanf("%d", &s[i].numShares);
        printf("Enter the Buying Price Per Share\n");
        printf(">");
        scanf("%f", &s[i].buyShare);
        printf("Enter the Current Price Per Share\n");
        printf(">");
        scanf("%f", &s[i].currPrice);
        printf("Enter the Yearly Fees\n");
        printf(">");
        scanf("%f", &s[i].fees);

        s[i].initCost = (float)s[i].numShares * s[i].buyShare;
        s[i].currCost = (float)s[i].numShares * s[i].currPrice;
        s[i].profit = s[i].currCost - s[i].initCost - s[i].fees;
        fflush(stdin);
    }
}

/* Sort the array of structures 
on stock name and print the array 
after the sort is completed */

void sort(struct Stock s[], int n)
{
    Stock t;
    for(int i=0;i<n-1;i++)
        for(int j=0;j<n-1;j++)
            if(strcmp(s[j].name, s[j+1].name)>0)
            {
                t=s[j];
                s[j]=s[j+1];
                s[j+1]=t;
            }
}

/* Calculate and print the total profit for all of the stocks. 
That is, find the sum of the 5 profits for each stock. In 
addition, find and print out the number of stocks that 
had a positive profit, the number of stocks that had a 
negative profit, and the number of stocks that broke 
even, that is had a profit of $0.00 */

void calc(struct Stock s[],int n)
{
    float total=0;

    int Pos=0;
    int Neg=0;
    int Even=0;

    for(int i=0;i<n;i++)
    {
        total +=s[i].profit;
        if (s[i].profit>0)
            ++Pos;
        else if (s[i].profit<0)
            ++Neg;
        else
            ++Even;
    }

    printf("\n");
    printf("%d of stocks broke Positive\n",Pos);
    printf("\t%d of stocks broke Negative\n",Neg);
    printf("\t\t%d of stocks broke Even\n",Even);
    printf("\n");
    printf("The Total Trofit is $%f\n", total); //Check output
    printf("\n");
}
//Output of the calc function
void print(struct Stock s[], int n)
{
    for(int i=0;i<n;i++)
    {
        printf("\n");
        printf("The stock is %s\n", s[i].name);
        printf("\tWith Initial cost of $%0.2f\n", s[i].initCost);
        printf("\t\tCurrent cost is $%0.2f\n", s[i].currCost);
        printf("\t\t\tAnd your Profit is $%0.2f\n", s[i].profit); //Check output
        printf("\n");
    }
}
//Save the array of structures to a text file.
void savetext(struct Stock s[], int n)
{
    FILE *f;
    f = fopen("c:\\cs36\\textfile.txt","w"); 
    int i;
    for(i=0;i<n;i++)
    {
        fprintf(f,"%s\n",s[i].name);
        fprintf(f,"%d  %f  %f  %f  %f  %f  %f\n", s[i].numShares, s[i].buyShare, s[i].currPrice, s[i].fees, s[i].initCost, s[i].currCost, s[i].profit);
    }
    fclose(f);
}
//Retrieve and print the text file.
void loadtext(struct Stock s[], int n)
{
    FILE *f;
    f = fopen("c:\\cs36\\textfile.txt","r");
    for(int i=0;i<n;i++)
    {
        fgets(s[i].name, sizeof(s[i].name), f);
        fscanf(f, "%d%f%f%f%f%f%f\n", &s[i].numShares, &s[i].buyShare, &s[i].currPrice, &s[i].fees, &s[i].initCost, &s[i].currCost, &s[i].profit);
    }
    fclose(f);
}
//Save the array of structures to a binary file.
void savebin(struct Stock s[], int n)
{
    FILE *f;
    f = fopen("c:\\cs36\\binfile.bin","wb");
    if(! f)
    { 
        printf("Could not open the file");
        exit(1); 
    }
    else
        fwrite(&s, sizeof(s[10]), n, f);
    fclose(f);
}
//Retrieve and print the binary file.
void loadbin(struct Stock s[], int n)
{
    FILE *f;
    f = fopen("c:\\cs36\\binfile.bin","rb");
    if (! f)
    { 
        printf("Could not open the file");
        exit(1); 
    }
    else
        fread(&s, sizeof(s[10]), n, f);
    fclose(f);
}

int main (void)
{
    printf("Hello, Dave\n");
    Stock s[SIZE];
    load (s, SIZE);
    sort (s, SIZE);
    savetext (s, SIZE);
    savebin (s, SIZE);
    print (s, SIZE);
    calc (s, SIZE);
    loadtext (s, SIZE);
    print (s, SIZE);
    loadbin (s, SIZE);
    print (s, SIZE);
    system("PAUSE");
}

This is C, using Visual C++ 2008 Express

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:21:24+00:00Added an answer on May 27, 2026 at 11:21 am

    You are trying to access the null pointer.So that it will throw this error.

        fprintf(f,"%s\n",s[i].name);
    

    In these line, s[i] may be null for some cases in your program so you are trying to access the member variable of the object which is NULL.

    So only it is throwing this runtime error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a very simple thing, but I can't see any examples
So I've been doing this all night - can't quite understand my homework, and
I'm doing a homework project that requires this: Below you will find the code
This one is a case of not doing your homework.:-) Apart from dynamic loading
I'm doing a basic homework assignment which looks like this: While input <> -1
This is NOT a homework question, actually I am doing this for fun. Here
This is a PDP-8 Program. Kindly can any one disassemble it. Binary representation Octal
This might seem a bit like a do-my-homework-for-me question (and it is), but I
This is a homework problem that I have. I have been doing some research
I would like to mention that this is a homework but am not asking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.