如何使用while循环返回启动程序?

时间:2014-11-21 13:29:02

标签: c while-loop

当我选择它时,我遇到了一个问题,总是退出我的程序,当我输入错误的选择时必须退出?

我的循环有什么问题?请帮助我循环我遇到严重问题。这是一个基本概念,但我的大脑已经老了,卡住了。

你能帮我理解循环以返回程序的开头。

谢谢

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
    char choice,name[40],date[40],note[500],;
    int password;
    int ch;
    printf("\n\n\t\tMAIN MENU:");
    printf("\n\n\tADD RECORD\t[1]");
    printf("\n\tVIEW RECORD\t[2]");
    printf("\n\tEDIT RECORD\t[3]");
    printf("\n\tDELETE RECORD\t[4]");
    printf("\n\tEDIT PASSWORD\t[5]");
    printf("\n\tEXIT\t\t[6]");
    printf("\n\n\tENTER YOUR CHOICE:");
    scanf("%d",&ch);

    switch(ch)
        do{
            {
                opcase 1:
                    printf("\n\tEDIT RECORD\t[3]");
                    printf("\n\tEDIT RECORD\t[3]");
                    printf("\n\tEDIT RECORD\t[3]");
                    printf("\n\tEDIT RECORD\t[3]");
                    break;

                case 2:
                    printf("\n\tEDIT RECORD\t[3]");                      
                    break;

                case 3:
                    printf("\n\tEDIT RECORD\t[3]");
                    break;

                case 4:
                    printf("\n\tEDIT RECORD\t[3]");
                    break;

                case 5: 
                    printf("\n\tEDIT RECORD\t[3]");
                    break;

                case 6:
                    printf("\n\n\t\tTHANK YOU FOR USING THE SOFTWARE BY:\n\n\tBIJAY PURI\n\n\tBHANU POUDEL\n\n\tNRIPASH AYER...");
                    getch();
                    exit(0);

                default:
                    printf("\nYOU ENTERED WRONG CHOICE..");
                    printf("\nPRESS ANY KEY TO TRY AGAIN");
                    getch();
                    break;
                }     
            }
            while(ch!=6);          
  return 0; 
  getch();
}

3 个答案:

答案 0 :(得分:3)

do..while循环置于switch个案例之外。

提示:如果你正确缩进代码,你可能已经自己解决了。

更正语法:

do 
  {

    //do something
        switch (variable)
        {
        //case 
        break;
        }
  }
while (condition)

编辑:

以下是您的代码的[在linux上]的工作版本。请仔细注意这些变化。

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

int main()
{
        char choice,name[40],date[40],note[500];
        int password;
        int ch;

        printf("\n\n\t\tMAIN MENU:");
        printf("\n\n\tADD RECORD\t[1]");
        printf("\n\tVIEW RECORD\t[2]");
        printf("\n\tEDIT RECORD\t[3]");
        printf("\n\tDELETE RECORD\t[4]");
        printf("\n\tEDIT PASSWORD\t[5]");
        printf("\n\tEXIT\t\t[6]");


        do{
        printf("\n\n\tENTER YOUR CHOICE:");
        scanf("%d",&ch);
                switch(ch)

                {
                        case 1:
                            printf("\n\tEDIT RECORD\t[3]\n");
                            printf("\n\tEDIT RECORD\t[3]\n");
                            printf("\n\tEDIT RECORD\t[3]\n");
                            printf("\n\tEDIT RECORD\t[3]\n");

                                break;
                        case 2:    
                                printf("\n\tEDIT RECORD\t[3]");                      
                                break;
                        case 3:
                                printf("\n\tEDIT RECORD\t[3]");
                                break;
                        case 4:
                                printf("\n\tEDIT RECORD\t[3]");
                                break;
                        case 5:            
                                printf("\n\tEDIT RECORD\t[3]");
                                break;
                        case 6:
                                printf("\n\n\t\tTHANK YOU FOR USING THE SOFTWARE BY:\n\n\tBIJAY PURI\n\n\tBHANU POUDEL\n\n\tNRIPASH AYER...");
                                exit(0);
                        default:
                                printf("\nYOU ENTERED WRONG CHOICE..");
                                printf("\nPRESS ANY KEY TO TRY AGAIN");
                                break;
                }
        } while(1);          //no need to check conditions here, redundant.

        return 0 ; 
}

答案 1 :(得分:1)

你的do whileswitch花括号纠结在一起。

这就是你所拥有的:

switch (variable)
{
    do {

    }
}
while (condition);

这是正确的方法:

do {
    switch (variable)
    {

    }
}
while (condition);

此外,您必须将getch放在return指令之前,否则它没有意义。

答案 2 :(得分:0)

你必须做类似

的事情
do{
   //read your option
   scanf("%d",&ch);
   //do something with that option you get
   switch(option){
   }
}while(option != 6);

这是因为总是要思考,你需要重复什么行动