Я написал этот код, чтобы сделать некоторую алгебру с мнимыми числами с помощью структур, но когда я запускаю его, я получаю это Что случилось с getchar в C, пожалуйста?
Что я делаю неправильно я пытался все различные способы, чтобы получить символ, я озадачен !!
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct complex { float Rez, Imz; };
int main()
{
char operand;
struct complex z1, z2;
printf("give the Real part of the first imaginary number z1:\n");
scanf("%f", &z1.Rez);
printf("zreal=%f",z1.Rez);
printf("give the Imaginary part of the first imaginary number z1: \n");
scanf("%f", &z1.Imz);
printf("give the Real part of the second imaginary number z2: \n");
scanf("%f", &z2.Rez);
printf("give the Imaginary part of the second imaginary number z2: \n");
scanf("%f", &z2.Imz);
do
{
printf("Give + for ADD\nGive * for MULTIPLICATION\nGive - for SUBTRACTION\nGive/for DIVISION\n");
operand=getchar();
if (operand!='+' || operand!='-' || operand!='*' || operand!='/')
printf("Wrong operation.Try again.\n");
}while(operand!='+' || operand!='-' || operand!='*' || operand!='/');
if(operand=='+')
{
printf("\nYou have chosen to add the numbers.\nThe result of the ADD is the inaginary number\n imz=(%f+%f) +(%f+%f)",z1.Rez,z2.Rez,z1.Imz,z2.Imz);
}
if(operand=='-')
{
}
if(operand=='*')
{
}
if(operand=='/')
{
}
system("pause");
return 0;
}
Пожалуйста, приложите любые предложения? Я застрял!
Это впечатляет, как никто из вас не пришел к идее использовать отладчик для проверки значения 'operand' после вызова' getchar' в течение трех часов ...;) – mafso
@mafso конечно 'getchar()' возвращает ' «\ n''. Лучше использовать с 'scanf ("% c ", & operand)' и получить '' '' '' '' 'и' 'right. – chux
@chux: Спойлер! :) – mafso