-2

I'm a beginner in programming. I made a simple program in C# but it doesn't work properly. When I type "rezistenta" it should run the condition if (valoare=="rezistenta") When I type "capacitate" it should run the second if: if(valoare=="capacitate") In both cases the program runs the last else, it skips the first 2 if conditions.

The program:

#define _CRT_SECURE_NO_WARNINGS  //directive preprocesor
#include<stdio.h>
#include<conio.h>


void main(void)
{
    char valoare[100];
    float C1, C2, CS, CP;
    float R1, R2, Rs, Rp;


    printf("\nCapacitate sau Rezistenta? ");
    scanf("%s", &valoare);
    printf("\nAti introdus= %s", valoare);

    if (valoare == "rezistenta")
    {
        printf("\nIntroduceti valorile rezistentelor: ");
        scanf("%f%f", &R1, &R2);
        Rs = R1 + R2;
        printf("\nRezistenta echivalenta serie este: *%6.3f*", Rs);
        Rp = (R1*R2) / (R1 + R2);
        printf("\nRezistenta echivalenta paralel: *%6.3f*", Rp);
    }
    else if (valoare == "capacitate")
    {
        printf("\nIntroduceti valorile capacitatilor: ");
        scanf("%f%f", &C1, &C2);
        CS = (C1*C2) / (C1 + C2);
        printf("\nValoarea capacitatilor serie este = *%-6.4f*", CS);
        CP = C1 + C2;
        printf("\nValoarea capacitatilor in paralel este= *%-6.4f*", CP);
    }
    else
        printf("\nSunteti nehotarat vi le dau pe amandoua");



    printf("\nIntroduceti valorile rezistentelor: ");
    scanf("%f%f", &R1, &R2);
    Rs = R1 + R2;
    printf("\nRezistenta echivalenta serie este: *%6.3f*", Rs);
    Rp = (R1*R2) / (R1 + R2);
    printf("\nRezistenta echivalenta paralel: *%6.3f*", Rp);

    printf("\nIntroduceti valorile capacitatilor: ");
    scanf("%f%f", &C1, &C2);
    CS = (C1*C2) / (C1 + C2);
    printf("\nValoarea capacitatilor serie este = *%-6.4f*", CS);
    CP = C1 + C2;
    printf("\nValoarea capacitatilor in paralel este= *%-6.4f*", CP);



    _getch();



}//end main
slugster
  • 49,403
  • 14
  • 95
  • 145

2 Answers2

5

are you sure this is C#? It look like C. For C, it use printf(""), but C# should be Console.WriteLine("") or Console.Write("")

Anyhow, if you are using C, you cannot do the string comparison like this:

if (valoare == "rezistenta")     //this is wrong

The correct way should be:

if(strcmp(valoare, "rezistenta") == 0)

Of course you have to include the library on top:

#include <string.h>

Please kindly have a try.

Harlo
  • 507
  • 2
  • 12
2

Here you can not compare like this way. Here

 if(strcmp(valoare,"rezistenta")==0)
{
//...
}

and include a header file

#include<string.h>