如何用C语言存储和更新数组字符串? [英] How do I store and update array string in C language ?

查看:71
本文介绍了如何用C语言存储和更新数组字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在交换机案例7下存储一个地址:在程序中。当我按下7时,必须显示先前存储的地址,并且还必须有一个更新新地址的选项。但是当我按7时没有任何反应。



我尝试了什么:



I wanted to store an address under switch case 7: in the program. when I press 7 an address must display which will be previously stored and there must be an option for updating new address also. But when I press 7 nothing happens.

What I have tried:

//Program to display student information
#include<stdio.h>
#include<stdlib.h>
int choice, enrol, batch;

char a[30]={"Any address"};
int i=0;

void main()

{
    do
    {
printf("\n---------------------Student Information Section--------------------------");
printf("\n\n1. Student Information\n");
printf("2. Course Material Despatch Status\n");
printf("3. Fee Status\n");
printf("4. Time Table For Theory Counselling\n");
printf("5. Time Table For Practical Counselling\n");
printf("6. Assignment Submission Schedule\n");
printf("7. Change Of The Correspondence Address\n");
printf("8. General Queries\n");
printf("9. Quit");
printf("-------------------------------------\n\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
while(enrol!=1234)
{
 printf("\nEnter Your Enrolment Number: ");
 scanf("%d", &enrol);
 if (enrol != 1234)
printf("\nPlease Enter a valid Enrolment No.\n");
}

    printf("\n\n********BCA PROGRAMME********");
    printf("\nCourse duration Minimum 3 years Maximum 6 Years");
    printf("\nYear of Study 2017");
    printf("\nRegistered Semester II");
    printf("\nStudy Center Guwahati");
    printf("\nRegional Center Guwahati");
    printf("\nFees paid For Per Semester 6000/-");
    printf("\nBatch No: 5");
    printf("\nClass duration will be one and half an hour");
    break;
case 2:
    printf("\nStudy material is despatched.");
    break;
case 3:
    printf("\nFees is Paid For Semester II: 6000/-");
    break;
case 4:
    while(batch!=5)
{
 printf("\nEnter Your Batch Number: ");
 scanf("%d", &batch);
 printf("\nTheory counselling will start from 9 AM\nPlease visit study centre for complete details.\n");
 if (batch != 5)
printf("\nPlease Enter a valid Batch No.\n");
}
break;
case 5:
    while(batch!=5)
{
 printf("\nEnter Your Batch Number: ");
 scanf("%d", &batch);
 printf("\nPractical counselling will start from 9 AM\nPlease visit study centre for complete details.\n");
 if (batch != 5)
printf("\nPlease Enter a valid Batch No.\n");
}
break;
case 6:
    printf("Last date of submission of assignment");
    printf("\nFor July-December Session is 15th october 2017");
    printf("\nFor January-June Session is 15th April 2018");
break;
case 7:
 while( (a[i++]=getchar()) != '\n' && i < 30) /* take input from user until it's a newline or equal to 30 */
        ;
     a[i] = '\0'; /* null-terminate the string */
     i = 0;
     while(a[i] != '\0') /* print until we've hit \0 */
         printf("%c",a[i++]);

break;
case 8:
    printf("\n---------Frequently Asked Questions-----------");
    printf("\n\n\nQ)What is Academic Schedule?");
    printf("\nAdmission to various programmes of the \nUniversity are open as per the Academic Calendar.");
    printf("\n\nQ)What is the fee Amount & Mode of the Payment?");
    printf("\nYou can pay using online portal or by bank draft.");
    printf("\n\nQ)When will I be notified of my acceptance?");
    printf("\nYou will be notified of your acceptance usually\nwithin two months after the receipt of your application.");
    printf("\n\nQ)What is an auto generated Control Number?");
    printf("\nAn auto generated Control Number is allotted\nprovisionally after submission of online application form.");
    printf("\n\nQ)What if I forget to take printout of Online Application Form after submission?");
    printf("You can download it again from the print application section.\n\n\n");
break;
case 9:
    exit(0);

default:printf("wrong choice\n");
}

}
while(choice>'1' || choice<'9');
}

推荐答案

请记住getchar()只在按下ENTER后才开始返回字符 - 所以直到用户输入包括ENTER,不会发生任何事件,因为不会收到任何字符。 getchar没有返回单独的按键!



代码可能第一次工作 - 因为你初始化 i 到当你声明它时为零=但在那之后它会做一些奇怪的事情。你真的需要在开始阅读角色之前立即将 i 重置为零。



自己动手 - 和其他人 - 一个很大的好处:正确缩进你的代码!这就是到处都是,这使得很难确切地知道究竟发生了什么。



最后,你有一个工具可以帮你找到发生了什么:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


抱歉,但我们不能为您做到这一点 - 时间让您学习一种新的(非常非常有用的)技能:调试!
Do remember that getchar() only starts returning characters once ENTER has been pressed - so until your user input includes ENTER, nothing will happen as no characters will be received. getchar does not return individual key presses!

The code might work the first time - because you initialise i to zero when you declare it = but after that it's going to do some odd things. You really need to reset i to zero immediately before you start reading the characters.

And do yourself - and everybody else - a big favour: indent your code properly! That is all over the place, and that makes it difficult to work out exactly what goes where.

Finally, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


避免使用对 scanf()和 getchar()同时用于用户输入或处理每个用户的特定特征。



特征是:



scanf()最后不会读取换行符输入但是在开头读取并忽略空格(包括换行符)。格式%c,%n或%[]有例外,它们不会读取和忽略前导空格。



getchar()将读取下一个可用字符。



了解这一点,您可以重现代码中发生的情况。您在函数顶部调用 scanf(),它允许输入缓冲区中有未读的换行符。再次调用 scanf() aftwerwards是没有问题的,因为它将首先读取并忽略该换行符。当调用 getchar()时,它会读取仍在输入缓冲区中的换行符,这会使你的循环终止。



通常的解决方案是只使用一种方法。



使用 scanf(%c)来读取单个字符。请注意%c格式的前导空格。这将让 scanf()读取并忽略前导空格,就像大多数其他格式一样。将字符串读入char缓冲区并限制最大值。要读取的字符数使用格式字符串中的宽度前缀:

Avoid using calls to scanf() and getchar() concurrently for user input or handle the specific characteristics of each.

The charateristics are:

scanf() will not read the newline at the end of an input but reads and ignores white spaces (which includes newlines) at the beginning. There are exceptions for the formats "%c", "%n" or "%[]" which will not read and ignore leading white spaces.

getchar() will read the next available character.

Knowing this you can reproduce what happens in your code. You are calling scanf() on top of your function which lets an unread newline in the input buffer. Calling scanf() aftwerwards again is no problem because that will read and ignore that newline first. When calling getchar() now it will read the newline still in the input buffer which makes your loop terminating.

The usual solution is to use only one method.

Use scanf("% c") to read a single character. Note the leading space in the " %c" format. That will let scanf() read and ignore leading white spaces too like with most other formats. To read a string into a char buffer and limit the max. number of chars to read use the width prefix in the format string:
scanf("%29s", a);



使用 getchar()使用 gets()或更好 fgets(stdin)来读取用户输入的完整行。两者都将读取换行符,但 gets()将不会将其存储在缓冲区中,而 fgets()将其存储在缓冲。建议使用 fgets(),因为它允许传递缓冲区大小。



无论使用何种方法在读取字符串时检查返回值,以了解用户输入的字符数太多。


With getchar() use gets() or better fgets(stdin) to read a complete line entered by a user. Both will read the newline but gets() will not store it in the buffer while fgets() stores it in the buffer. The usage of fgets() is recommended because it allows passing the buffers size.

Regardless of the used method check the return values when reading strings to know when a user has entered too many characters.


这篇关于如何用C语言存储和更新数组字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆