开发者

allowing users to key in multiple lines in command prompt by using C

I don't really know about C. So, my questions and ideas may be misleading or ridiculous or embarrassing. Please forgive me..

The question states that the program should get the user input from the command prompt. the input text will have numbers, alphabets and space characters. they will have multiple lines too..

The example program is using <stdio.h> and hence printf and scanf.

Using cin and cout is not encouraged according to the textbook. In order to use cin and cout, I need <iostream> but I can't include <iostream>. Even if I could include <iostream>, but I then can't include <stdio.h> anymore.

I also can't use string. I've heard that C doesn't allow the use of string, but I don't really understand why.


My question is; when you ask users to type in something with printf("Enter your text: ");, they can only type in one line.. once they hit Enter, the program receive it.

Is there anyway to use scanf to allow users to type in multiple lines by typing Shift+Enter for example.

As string is not available, should I use a char array like char inputText[999]? Will this give me enough space?

My two questions are; How to allow users to type multiple lines in command prompt, and what data t开发者_StackOverflow中文版ype should I use to save the entered text?

I googled it but they mix C++ and C..

Thanks to all and once again, i apologize if my question is a waste of time for you..

Hi all, I have done it.. but I only have one more problem..

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

int nc=0,nw=0,bs=0,c, nq=0, nl=0;
int main()
{
    printf("Text Analysis Program\n\n");
    printf("Enter your text:");
    while((c=getchar())!=EOF)
    {
        if( (c>='A' && c<='Z') || (c>='a' &&  c<='z') )
        {
            nc++;
        }
        else if( c>='0' && c<='9' )
        {
            nq++;
        }
        else if (c==' ')
        {
            nw++;
            bs++;       
        }
        else if (c=='\n')
        {
            nw++;
            nl++;
        }
    }
    printf("Number of characters: %d;\n",nc);
    printf("Number of words: %d;\n",nw);
    printf("Number of numerical quantities: %d;\n",nq);
    printf("Number of blank spaces: %d;\n",bs);
    printf("Number of lines entered: %d;\n",nl);
    exit(0);
}

At the end, after the answers are shown, cmd screen closes straight away.. I removed exit(0) it is still the same. Is there any commands to freeze the screen and appears Press any key to continue


These:

#include <iostream>
#include <string>
cin
cout

are C++-specific. They simply don't exist in C. Remember that C and C++ are two different languages.

The fact that your window is closing when the program terminates is a function of the development system you're using. If you run the program from a command prompt, that won't happen -- or there may be an option in your IDE to tell it to keep the window open until you close it explicitly. But if you don't want to do that, J.F. Sebastian's solution is a good one.

Another hint: take a look at the isalpha() and isdigit() functions in <ctype.h>.


Just add another getchar() call:

#include <stdio.h>

int main() {
  printf("Press any key to continue. ");
  fflush(stdout);
  getchar();
  return 0;
}

It actually doesn't return until you type a full line or it hits EOF but it should be good enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜