2015-04-30 42 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 ,, –