2015-04-30 39 views
3

我想写一个简单的程序来找出不同形状的区域。该程序编译得很好,但是当它运行时它不会给我正确的答案。我的意思是当它运行它时会问:如果用于字符串比较的语句没有正确执行

你想找什么区域?

,当我键入

或其他任何东西,然后回车,它只是结束,它不执行任何其他代码。

代码如下。

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

int main() 
{ 
    char yourchoice[40]=""; 
    float a=0;; //a=height 
    float b=0; //b=breadth 
    float Sq,Rec,Parall,trap,cir,ell,tri; 

    printf("Here You can find out areas of Square,Rectangle,Parallelogram,trapezoid,circle,ellipse and triangle \n\n\n"); 

    printf("what do u want to find area of? : \n"); 
    scanf(" %s",yourchoice); 

    if(yourchoice[40]== 'square'){ 
     printf("Length of any side of the square ="); 
     scanf(" %f",&a); 
     Sq = pow(a,2); 
     printf("Area of the Square is %f",&Sq); 
    }else if(yourchoice== 'rectangle') 
    { 
     printf("Height = \n"); 
     scanf("%f",a); 
     printf("Width=\n"); 
     scanf("%f",b); 
     Rec= a*b; 
     printf("Area of the Rectangle : %f ",&Rec); 

    } 
    return 0; 
} 
+2

我从来不明白的是为什么类在C使用字符串I/O第一初学者。有没有什么开始与哪个更适合?毕竟,它是一种系统编程语言,而不是一些通用的高级应用程序语言。那么为什么要在一开始就打扰字符串呢? – BitTickler

+2

另请参见:删除关于此行的'printf'语句 – Keith

+0

中的'&':'char yourchoice [40] =“”;'这导致您的选择包含:'\ 0'。 + 30个字节的垃圾。一个更好的初始化将是:char yourchoice [40] = {'\ 0'};它用'\ 0'(NUL)字符填满你的选择。 – user3629249

回答

4

点1:

使用strcmp()的字符串比较,不==运营商。

点2:

不管怎么说,为了char yourchoice[40]="";阵列,使用yourchoice[40]超出限制继而调用undefined behaviour的。从0

点3 C开始数组索引:

printf()并不需要有一个指针的数据的参数。改变

printf("Area of the Square is %f",&Sq); 
printf("Area of the Rectangle : %f ",&Rec); 

printf("Area of the Square is %f\n", Sq); //& not required 
printf("Area of the Rectangle : %f\n",Rec); //& not required 

点4:

scanf()需要一个指针数据类型参数.Change

scanf("%f",a); 
scanf("%f",b); 

scanf("%f", &a); // & is required 
scanf("%f", &b); // & is required 

点5:与感谢先生@ Mints97

您需要使用" "表示一个字符串字面' ' 用于表示char。这两个是不同的。

一般建议:

  1. 推荐的原型main()int main(void)
  2. 总是初始化所有的局部变量。
+0

非常感谢你兄弟,真的很感激,顺便说一句...... 我们只使用==在c比较整数和单个字符到左侧?? –

+0

@gurpindersingh请参阅[this](http://en.cppreference.com/w/c/language/operator_comparison)for一些澄清。如果你使用字符串作为'=='操作符的操作数,它们的基地址会被共同映射,而不是它们的内容。希望能够说清楚。 :-) –

+0

邑,明白了,thnxx –

1

你的代码有很多错误。比较字符串使用strcmp而不是==

if(yourchoice[40]== 'square')

应该

if(0 == strcmp(yourchoice, "square")) 
+0

您的语句没有任何错误,它是100%正常,但从偏好的角度来看,我通常阅读它'if(strcmp(yourchoice,“square”)== 0)'。那通常是因为你认为**“如果......又是什么?”**,哦,'if(strcmp ... ',而不是'if(0 ...'完全取决于你。 –

+0

感谢兄弟...欣赏 –

+1

我更喜欢总是将文字放在那么当比较应该是'=='但只输入'='时,编译器会捕获错误,而不是必须调试代码才能找到问题。 I.E.当我可以让编译器完成这项工作时,我会放过它。说到编译器,始终在启用所有警告的情况下进行编译,然后修复警告。 (和张贴代码有一堆警告,需要修复。 – user3629249

1

你不能==比较C字符串。你需要strcmp()。而''用于单个字符,而不是字符串。

所以,改变

if(yourchoice[40]== 'square') 

if(!strcmp(yourchoice, "square")) 

else if(yourchoice== 'rectangle') 

else if(!strcmp(yourchoice, "rectangle")) 

BTW,你需要包括<string.h>strcmp()

另外,更改

printf("Area of the Square is %f",&Sq); 

printf("Area of the Square is %f", Sq); 
           ^
            no need of & 

printf("Area of the Rectangle : %f ",&Rec); 

printf("Area of the Rectangle : %f ",Rec); 

当您在标识符之前加上&时,它会返回该标识符的地址。你不需要在printf()

+0

谢谢你Arun白... –

2

使用&当您使用

if(yourchoice[40]== 'square') 

yourchoice[40]只有你是一个字符串比较单个字符。
即使这是错误的,因为您声明char yourchoice[40]意味着索引将从039。 使用strcmp函数来比较字符串。
如果字符串相同,则将返回0,否则返回1-1
使用

if(strcmp(yourchoice, "square") == 0) 

或者

if(!strcmp(yourchoice, "square")) 

而在你printf声明不使用&打印变量的值。

改变这些线

printf("Area of the Square is %f",&Sq); 
printf("Area of the Rectangle : %f ",&Rec); 

printf("Area of the Square is %f",Sq); 
printf("Area of the Rectangle : %f ",Rec); 

而在你的其他部分,你忘了你的scanf

更改添加&这些行

scanf("%f",a); 
scanf("%f",b); 

scanf("%f",&a); // in scanf, '&' is required. 
scanf("%f",&b); 
+0

感谢兄弟.... 真的很感激... 我刚刚开始学习C,它似乎像bhool-bhoolaya :( –

+0

@gurpindersingh,欢迎:) – Himanshu

+0

C只有7个语句和一堆系统调用,所以学习7个语句,然后总是查找系统调用的语法,直到你完全熟悉这些调用。 (我一直在编程C约20年,仍然查找系统调用,我很少使用。) – user3629249

0
#include <stdio.h> 
    #include <stdlib.h> 
    #include <math.h> 
    #include <string.h> 

    using namespace std; 

    int main() 
    { 
    char yourchoice[40]=""; 
    float a=0;; //a=height 
    float b=0; //b=breadth 
    float Sq,Rec,Parall,trap,cir,ell,tri; 

    printf("Here You can find out areas of Square,Rectangle,Parallelogram,trapezoid,circle,ellipse and triangle \n\n\n"); 

    printf("what do u want to find area of? : \n"); 
    scanf(" %s",yourchoice); 

//Here, strcmp is the prefered way to compare the strings 
//string.h is the header to use it 
// It returns 0 when the string match 
    //if(yourchoice == 'square'){ 
    if(!strcmp(yourchoice,"square")){ 
     printf("Length of any side of the square ="); 
     scanf(" %f",&a); 
     Sq = pow(a,2); 
//Second correction: & are to used only when storing the input and not while accessing or displaying it.So, in scanf you have to use but not for printf statements 

    printf("Area of the Square is %f",Sq); 
    } 
    else if(!strcmp(yourchoice,"rectangle")) 
    { 
     printf("Height = \n"); 
//Third Correction, in the org code you had missed to write "&" before the variable 'a' 
     scanf("%f",&a); 
     printf("Width=\n"); 
//Fourth Correction, in the org code you had missed to write "&" before the variable 'b' 
     scanf("%f",&b); 
     Rec= a*b; 
//Fifth correction: Same as second correction. Not to use '&' sign in printf statements 

     printf("Area of the Rectangle : %f ",Rec); 

    } 
    return 0; 
    } 
+4

这是什么?哪里有解释你为什么发布这段代码。我建议你解释它的目的,否则别人可能不会发现它是一个信息性的答案。与其他答案进行比较。 –

+0

@ DavidC.Rankin:谢谢指出。 – letsBeePolite

+0

非常感谢@skn ,, –