I'm writing a C program on visual studio. But my program doesn't run nor it returns any errors. Can someone take a look at it?

user10109860

I hopelessly tried to copy paste this into another file but it still doesn't return anything.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#pragma warning (disable: 4996)

typedef struct item
{
    char *pName;
    int Quantity, Price, Total;
} ITEM;

ITEM *Exam(char *pInput);

int main()
{
    ITEM *pItem;
    char input[81];
    printf("Type items: ");
    gets_s(input);
    char *pInput = input;
    pItem = Exam(pInput);
    printf("Name: %s\nQuantity: %d\nPrice: %d\nTotal: %d\n", pItem->pName, pItem->Quantity, pItem->Price, pItem->Total);
    free(pItem);
    return 0;
}

ITEM *Exam(char *pInput)
{
    ITEM *pItem = (ITEM *)malloc(sizeof(ITEM));
    char *pNam, *pQuantity, *pPrice;
    int total, l, q, p;
    int qu, pr;

    for (l = 0; *(pInput + l) != ','; l++);
    pNam = (char *)malloc(l + 1);
    *(pInput + l) = '\0';
    strcpy(pNam, pInput);
    pItem->pName = pNam;
    *(pInput + l) = ',';

    for (q = 0; *(pInput + l + 2 + q) != ','; q++);
    pQuantity = (char *)malloc(q + 1);
    *(pInput + l + q + 2) = 0;
    strcpy(pQuantity, pInput + l + 2);
    qu = atoi(pQuantity);
    pItem->Quantity = qu;

    for (p = 0; *(pInput + l + q + 4) != ';' || *(pInput + l + q + 4) != 0; p++);
    pPrice = (char *)malloc(p + 1);
    *(pInput + l + q + 4) = 0;
    strcpy(pPrice, pInput + l + q + 4);
    pr = atoi(pPrice);
    pItem->Price = pr;
    pItem->Total = pr * qu;
    return pItem;
}

input is 'shirt, 100, 5'. I want the output to be a structure of name, quantity, price and total. It let me type the input but when i press enter it doesn't return anything. The program just hangs...

bruno

The last for in Exam is :

for (p = 0; *(pInput + l + q + 4) != ';' || *(pInput + l + q + 4) != 0; p++);

that for never end because its condition is always true

  • if *(pInput + l + q + 4) values ';' the test is false || true then true
  • if *(pInput + l + q + 4) values 0 the test is true || false then true
  • for all the other characters the test is true || true so again true
  • + 4 must be + 3 else goes 1 character too far
  • and p++ has no effect at all on the test

The test can be changed to !(*(pInput + l + q + 3 + p) == ';' || *(pInput + l + q + 3 + p) == 0) to stop when ';' or the null character is reached

Of course after *(pInput + l + q + 4) = 0; must be *(pInput + l + q + 3 + p) = 0;

Note that pQuantity and pPrice are allocated but never freed.


Exam can be simplified, for instance :

ITEM *Exam(const char *pInput)
{
    ITEM *pItem = (ITEM *)malloc(sizeof(ITEM));
    const char *p;

    p = strchr(pInput, ',');
    pItem->pName = strndup(pInput, p - pInput);
    p += 1;

    pItem->Quantity = atoi(p);
    p = strchr(p, ',') + 1;

    pItem->Price = atoi(p);
    pItem->Total = pItem->Price * pItem->Quantity;

    return pItem;
}

Note that the input string is not modified (I moved it const) and there is no dynamic allocation except for the result and the name


if you do not have strndup :

char * strndup(const char * s, int n)
{
    char * r = (char *) malloc(n + 1);

    memcpy(r, s, n);
    r[n] = 0;
    return r;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my window form look different when I run the program in Visual Studio?

Can't run a C++ program on Visual Studio 2013

I can't run my Ruby program and I see errors in the terminal. I can't find the program

The database that is Connected for my Visual Studio program, does not take the data into the tables when run the program

Why can't I run this C program?

I can't seem to get my C program to take inputs (Rectangular Prism Calculator)

View Doesn't Open When I Try To Run My Program?

How can I compile and run c# program without using visual studio?

C++ Visual Studio Won't fully run program

Homework problem on Java: I'm trying to run my program, it shows no error however when I try to run it it doesn't work?

My C++ program won't compile on Visual Studio code

Can run program with Mono but not with Visual Studio Mac

Why is my program not showing any output? 0 errors, 0 warnings but no output? I'm using Dev C++ compiler

Can someone explain me why is this C program compiling without errors?

Why writing anything after the fileName of a #include directive does't give any errors in a C program?

Why doesn't my program take input inspite of the fgets part? Any other suggestions are welcome

I can't figure out why command prompt is skipping inputs to a simple C++ program in Visual Studio

c program doesn't give me errors

How to run C program in Visual Studio with "parameters"

How to run a C program in Visual Studio Code?

After Convert Visual Studio 2013 project to Visual Studio 2010 my program can't read Database

Why can't I run a C program built on alpine on ubuntu?

Can someone access my locally run website even if I haven't specified any port forwarding?

Any ways to run program debugging in Visual Studio on NVIDIA graphics card?

I can't get an answer out of my C++ program

Why can't I use "%[^\n]" scanset on my c program?

How do I run my C program?

How do I see the time it took to run my program in Visual Studio Code?

I can't run a program in Ubuntu 14.04