English 中文(简体)
C Booking Issues
原标题:C Booking Issues
  • 时间:2011-11-22 17:43:43
  •  标签:
  • c
typedef struct contact {    

char firstname [40];    
char lastname [40]; 
char address [100]; 
char phone[10];

}contact;

int main ()
{
FILE *pFile;   
contact entry = {"", "", "", ""};
int choice;
char cont = 5;  

pFile = fopen("C:\contacts.txt", "w+");

if(!pFile){
    printf("File could not be open");
    return 1;
    }

printf("Choose a selection

");
printf("1. Enter First Name
");
printf("2. Enter Last Name
");
printf("3. Enter Address
");
printf("4. Enter Phone Number

");
scanf( "%d", &choice);

while (choice = 1|2|3|4|cont){   
    if (choice = 1){
        printf ("First name: ");      
        fgets(entry.firstname, sizeof(entry.firstname),stdin); 
    }
    else if(choice = 2){
        printf ("Last name: ");   
        fgets(entry.lastname, sizeof(entry.lastname),stdin);      
    }
    else if(choice = 3){
        printf ("Address: ");     
        fgets(entry.address, sizeof(entry.address),stdin);  
    }
    else if (choice = 4){
        printf ("Phone number: ");    
        fgets(entry.phone, sizeof(entry.phone),stdin);
    }
    else
        printf("Exiting");
        break;

    fwrite (&entry, sizeof (struct contact), 1, pFile); 
    printf ("Would you like to enter a new contact? (y/n)");    
    scanf ("%d", &cont);

    if (cont =  n | N )
        return 0;
}

fclose(pFile);

getchar();
return 0;
}

我的法典目前是我的法典。 我每次都选择1 2、3、4、进入门口和进入窗户。 如果逻辑正确,任何建议都受到欢迎,但“种子”对我来说是好的,但显然我需要另一套眼睛。 我想,在我没有必须进入档案中每个人的所有条目的地方。 此外,我要指出,我最初仅仅因为抱怨而继续开会,我知道不好的做法。 任何有用的信息都值得赞赏。

最佳回答

页: 1 您认为:

else if (choice = 4){
    printf ("Phone number: ");    
    fgets(entry.phone, sizeof(entry.phone),stdin);
}
else
    printf("Exiting");
    break;

即便您放弃了<条码>的,也不属于其他条款。 因此,无论在什么地方,break<>>>>>> /code 都会被执行,而贵方案则会中断。

为加以确定,在<条码>范围内添加条码,以备该休息。

else if (choice = 4){
    printf ("Phone number: ");    
    fgets(entry.phone, sizeof(entry.phone),stdin);
}
else
{
    printf("Exiting");
    break;
}

一旦您确定,这一条线将造成您的方案终止,因为它始终评价到<条码><>真实性/代码>和<条码>主要收益:

if (cont =  n | N )
    return 0;

你们想要这样说。

if (cont ==  n  || cont ==  N )
    return 0;

这些固定办法至少将阻止你的方案终止,但正如其他人所指出的,其他地方有许多逻辑错误,会妨碍它做你想要的东西。

问题回答

例如:

while (choice = 1|2|3|4|cont){   

对一些基本概念产生误解。

第一<代码>=assignment营运人。 除其他外,上述代码修改了<代码>choice的价值。 平等比较使用<代码>=。

其次,<代码>>>/代码>的操作者为bitwise或。 <代码>7。 (我请你说明为什么有些时间。) 相反,使用<条码>>>

while (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == cont) {

There are other similar errors throughout your code.

单一<代码>=在C.if (a = 5){* 始终执行!

页: 1 因此:

if (choice = 1){

if (choice == 1){

另一回事:

while (choice = 1|2|3|4|cont){   

你没有做什么。 它实际计算了双向或1、2、3、4和<代码>。 (仅改动==就足够了。) 你们需要比较每个价值:

while (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == cont){ 

并通知使用<条码><>>>>(代号>)而不是双向表示。

EDIT: The reason your program prematurely exits is because of the following:

   else
        printf("Exiting");
        break;

({>>>>>>>>),因此,实际上这意味着(尽管有误导性缺陷):

   else
        printf("Exiting");
   break;

你的法典可能有更多的错误。

通过使用<代码>if(choice = 1),你说“如果我选择了1”,这实际上保证了工作,但销毁了以往的价值选择。

您希望从<代码>if(choice=1)开始,这意味着 “如果我把选择与选择二者作一比较,这是否平等?”





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签