1
votes

I can't seem to figure out what is wrong in my code, when It comes to the first scanf in do while loop, I enter a number and it just stops there.

If I put printf("Something"); after that scanf, it isn't printed so it isn't a infinite loop. Also program does not just freeze, when I go into Task Manager, it takes up CPU, so it is doing something.

My program is supposed to load resistors and calculate their parallel equivalent, until I type in 'done', and then print out the calculated number.

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

typedef struct o{char om[20];}RESISTOR;

int finish(char *s)
{
    if( s[0]=='d' && s[1]=='o' && s[2]=='n' && s[3]=='e' && s[4]==0 )
        return 0;
    else return 1;
}

int power(int n, int pows)
{
    int expo=1;
    while (pows)
    {
        expo*=n;
        pows--;
    }
    return expo;
}

int convert(char *s)
{
    int broj,c;for(c=-1;s[c];c++);
    for(int i=0;s[i];i++)
        {broj+=(s[i]-0x30)*power(10,c-i);}
    return broj;
}

double paralel(double old, int new)
{
    double num;
    num=((double)new*old)/(old+(double)new);
    return num;
}

int main()
{
    int n=0;double para;
    RESISTOR *p=(RESISTOR *)malloc(1*sizeof(RESISTOR));
    int *convertnum=(int *)malloc(1*sizeof(int));
    do
    {
        printf("R%d= ",n+1);
        scanf(" %s", (p+n)->om);
        convertnum[n]=convert((*(p+n)).om);
        if(n==0) para=convertnum[n];
        else if (finish((*(p+n)).om)) para=paralel(para,convertnum[n]); 
        n++;
        if(finish((*(p+n-1)).om))
            {
                p=(RESISTOR *)realloc(p, n*sizeof(RESISTOR));
                convertnum=(int *)realloc(convertnum, n*sizeof(int));
            }
    }while(finish((*(p+n-1)).om));

    printf("\n");printf("\n");
    printf("Re= %.2f",para);
    free(p);free(convertnum);
    printf("\n");
    return 0;
}
1
for(c=-1;s[c];c++); accesses s[-1], which might be 0, which means c-i in power(i,c-i) is negative, and thus the while(pows){pows--} has to go through 2^64 loops which may take a while. Your program should not take any measurable cpu time. Btw, you do n++ in the do..while, accessing convertnum[n], but you only malloc(1*sizeof(int)). Oh, printf("Something\n"); after the scanf should show some output.Kenney
Ah, correct. I didn't think about it, I needed to decrement c so I put it there. That makes the program run fine now, and show other errors :D. You should have put it as an answer.Nebeski
Also n is 0 there, so how should convertnum[0] be a problem? In the first loop I mean.Nebeski
No, not a problem - you allocate one. But when n=1, your program behaves in an undefined way.Kenney
But you see there in the finish, I say (p+n-1) and the other n in realloc should be there incremented by one..Nebeski

1 Answers

1
votes

for(c=-1;s[c];c++); accesses s[-1], which might be 0, which means c-i in power(i,c-i) is negative, and thus the while(pows){pows--} has to go through 2^64 loops which may take a while. Your program should not take any measurable cpu time.