2014-10-06 100 views
0
#include<stdio.h> 
int main() 
{ 

int choice; 
printf("Enter 1 for Programmers Name and ID\n"); 
printf("Enter 2 to Perform Integer Operation\n"); 
printf("Enter 3 to Perform Floating Point Operation\n"); 
scanf("%d", &choice); 
system("CLS"); 

if (choice == 1) 
    printf("Connor \n000000000\n"); 

else if (choice == 2) 
{ 
    char c; 
    int num1, num2; 
    printf("Enter operator:"); 
    scanf("%c", &c); 
    getchar(); 
    printf("Enter two integer's :"); 
    scanf("%d %d", &num1, &num2); 

    switch (c) 
    { 
    case '+': 
     printf("%d + %d = %d", num1, num2, num1 + num2); 
     break; 
    case '-': 
     printf("%d - %d = %d", num1, num2, num1 - num2); 
     break; 
    case '*': 
     printf("%d * %d = %d", num1, num2, num1*num2); 
     break; 
    case '/': 
     printf("%d/%d = %d", num1, num2, num1/num2); 
     break; 

    default: 
     printf("The value of c = '%c'\n"); 
     system("pause"); 
     return(0); 
    } 
} 
else if (choice == 3) 
    printf("Enter two \n"); 


system("pause"); 
return(0); 
} 

我需要一點幫助,找出這個代碼的操作部分的一個小問題.......一切正常,因爲可以放在操作符和整數,但我沒有得到交換機的輸出。開關盒程序不給出輸出

+1

嘗試把'在每個的printf的端\ N'。 – user3386109 2014-10-06 23:10:21

+0

@ user3386109他沒有得到任何輸出......所以\ n不是問題 – Steve 2014-10-06 23:11:01

+2

沒有讀過它,但添加一個'default'子句並打印出'c'的值將是一個好的開始的調試。 – 2014-10-06 23:11:59

回答

0

這應該肯定的工作

#include<stdio.h> 
int main() 
{ 

int choice; 
printf("Enter 1 for Programmers Name and ID\n"); 
printf("Enter 2 to Perform Integer Operation\n"); 
printf("Enter 3 to Perform Floating Point Operation\n"); 
scanf("%d", &choice); 
getchar(); 
if (choice == 1) 

    printf("Connor \n000000000\n"); 


else if (choice == 2) 


{ 
    char c; 
    int num1, num2; 
    printf("Enter operator:"); 
    scanf("%c", &c); 
    printf("Enter two integer's :"); 
    scanf("%d %d", &num1, &num2); 
    switch (c) 
    { 

    case '+': 
     printf("%d + %d = %d", num1, num2, num1 + num2); 
     break; 
    case '-': 
     printf("%d - %d = %d", num1, num2, num1 - num2); 
     break; 
    case '*': 
     printf("%d * %d = %d", num1, num2, num1*num2); 
     break; 
    case '/': 
     printf("%d/%d = %d", num1, num2, num1/num2); 
     break; 

    default: 
     printf("The value of c = '%c'\n" , c); 
     return(0); 
    } 

} 
else if (choice == 3) 
    printf("Enter two \n"); 

} 
+0

那裏我相信一切應該是爲了 – 2014-10-07 01:04:15

+0

它工作嗎? – 2014-10-07 01:05:15

+0

否....在這裏,它是如何運行按2>進入操作+>輸入整數2 2>(導致默認) – 2014-10-07 01:07:21

-1
  1. 變化:

    scanf("%c", &c); 
    getchar(); 
    

    到:

    scanf(" %c", &c); 
    
  2. 添加\n每個這些:

    printf("%d + %d = %d\n", num1, num2, num1 + num2); 
            ^^ 
    
  3. 實際上提供char當你告訴printf()打印一個:

    printf("The value of c = '%c'\n", c); 
               ^^^ 
    

,它應該爲你工作。修改後的代碼,刪除所有system()無義:

#include <stdio.h> 

int main(void) 
{ 
    int choice; 

    printf("Enter 1 for Programmers Name and ID\n"); 
    printf("Enter 2 to Perform Integer Operation\n"); 
    printf("Enter 3 to Perform Floating Point Operation\n"); 
    scanf("%d", &choice); 

    if (choice == 1) { 
     printf("Connor \n000000000\n"); 
    } 
    else if (choice == 2) { 
     char c; 
     int num1, num2; 

     printf("Enter operator:"); 
     scanf(" %c", &c); 

     printf("Enter two integers :"); 
     scanf("%d %d", &num1, &num2); 

     switch (c) { 

     case '+': 
      printf("%d + %d = %d\n", num1, num2, num1 + num2); 
      break; 
     case '-': 
      printf("%d - %d = %d\n", num1, num2, num1 - num2); 
      break; 
     case '*': 
      printf("%d * %d = %d\n", num1, num2, num1 * num2); 
      break; 
     case '/': 
      printf("%d/%d = %d\n", num1, num2, num1/num2); 
      break; 
     default: 
      printf("The value of c = '%c'\n", c); 
      break; 
     } 
    } else if (choice == 3) { 
     printf("Enter two \n"); 
    } 
    else { 
     printf("Invalid choice.\n"); 
    } 

    return 0; 
} 

與輸出樣本:

[email protected]:~/src/sandbox$ ./cal 
Enter 1 for Programmers Name and ID 
Enter 2 to Perform Integer Operation 
Enter 3 to Perform Floating Point Operation 
2 
Enter operator:* 
Enter two integers :4 6 
4 * 6 = 24 
[email protected]:~/src/sandbox$ 
+0

謝謝你的幫助對不起,它來到你不得不這樣做 – 2014-10-07 01:17:40

+0

沒問題,我們都必須從某個地方開始,你做了一個很好的嘗試。處理輸入/輸出是學習C的一個棘手部分。 – 2014-10-07 01:19:06