Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <stdio.h>
int i, n;
struct add_stock
{
    char fullname[30];
    int stocks;
    char com_name[30];
    int shares;
    float price;
    float total;
    int totalmoney;

} add;
int main()
{

    printf("Enter full name : ");
    scanf(" %[^\n]s", add.fullname);
    printf("Enter the no. of stocks you want to purchased : ");
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {

        printf("Enter the name of the company : ");
        scanf(" %[^\n]s", add[i].com_name);

        printf("Enter the no. of shares you want to purchased : ");
        scanf("%d", &add[i].shares);

        printf("Enter the price of each share  : ");
        scanf("%f", &add[i].price);

        add.total = add.shares * add.price;

        printf("Toatl money invested in this stock : ");
        scanf("%f", &add[i].total);
    }
    printf("Total money invested : ");
    scanf("%d", add.totalmoney);

    return 0;
}


What I have tried:

In question it's add_stock , not add stock , i write like this becoz it's not accepting question.
So, i get error for "add" saying subscripted value is neither array nor pointer nor vector.
Posted
Updated 27-Apr-22 6:56am
v2

1 solution

Your struct add is a single structure but you are trying to refer to items as if it was an array. You will need to allocate space for the number of stock structures after you get the number form the user. Something like:#
C++
struct add_stock* stocks = (struct add_stock*)malloc(sizeof(struct add_stock) * i);
// you then refer to the structures as: stocks[i]
 
Share this answer
 
Comments
CPallini 27-Apr-22 12:59pm    
5.
xguybluehat 27-Apr-22 13:06pm    
ok , thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900