-8

I am doing some training exercises to get used to C++ on Windows as a new programmer.

Take input from the user of type String and form:

    (any word) || (any word || <any word || any word> || (any word> etc.

Warn the user in real-time, if the:

    () or/and <>  Are closed before the input ends.

For example, if the user types:

    (I am ok

I have to warn him at that time that a parenthesis has been opened but hasn't yet been closed.

Since I am a beginner I am a bit lost about how to proceed, I wouldn't prefer a ready solution but rather a way to proceed.

  • I use code blocks IDE and GCC compiler.

Thanks in advance.

Georgez
  • 53
  • 9
  • Linux, Mac, WIndows,... ? – Jabberwocky Oct 30 '15 at 12:34
  • 1
    You do know there are functions to read one character at a time? You might want to [checkout this C input/output reference](http://en.cppreference.com/w/c/io). Even if you are only allowed to use [`scanf`](http://en.cppreference.com/w/c/io/fscanf) you can use that to read a single character at a time as well. – Some programmer dude Oct 30 '15 at 12:36
  • `std::flush` is actually a C++ function: http://stackoverflow.com/questions/14105650/how-does-stdflush-work. You were looking for [`fflush`](http://en.cppreference.com/w/c/io/fflush) (but note that this is for flushing entire lines and not on a per-character base). – Jongware Oct 30 '15 at 12:39
  • _"...this excersise is by far out of the things that our professor is taughting us all this time..."_. I **strongly doubt** it's true. I'm sure he explained everything you need, what's missing is your brain thinking about the problem but that's the point of homework... – Adriano Repetti Oct 30 '15 at 12:42
  • 1
    @AdrianoRepetti It really does seem like the teacher is looking for a non-standard solution, in which case the teacher's brain is the one with questionable activity. – Lundin Oct 30 '15 at 12:52
  • @Lundin I fail to see where anything _non-standard_ is required (but probably just because question is too vague)... – Adriano Repetti Oct 30 '15 at 12:57
  • 1
    @AdrianoRepetti It is simple: which standard console input function allows interruptions before the user presses enter? Which standard console output function allows you to print elsewhere, other than where the user is currently typing the input? – Lundin Oct 30 '15 at 13:00
  • @ Adriano Repetti im not raging at teacher , but is true that he hasnt tell as anything about this kind of IO. I think he just wants us to think by our own and to search by our own , thats not bad thing. – Georgez Oct 30 '15 at 13:00
  • 1
    @user2719403 Forcing beginners to run off into the woods looking for non-standard solutions is not a good way of teaching C programming. At any rate, you need to edit the post and specify which compiler you are using and system you are programming for. – Lundin Oct 30 '15 at 13:02
  • @Lundin one comes to my mind to process input on-the-fly: getchar(). About _write elsewhere_...it's not what question is asking for. User must be _warned_ and he doesn't say how. Yes it may be writing elsewhere (non-standard but what about ncurses? I see you already mentioned it) or printing _elsewhere_. What about stderr? It's allowed and standard. Good or not to teach C using non-standard function... **that's not the point**. If teacher introduced a putsat() function then OP has what he needs to complete his task (I'm not here to judge teacher capabilities from OP question...) – Adriano Repetti Oct 30 '15 at 13:14
  • @AdrianoRepetti Just like any other standard input function, getchar() will need an EOF (enter press) to return. – Lundin Oct 30 '15 at 13:37
  • @Lundin getchar() won't wait for \n to return... – Adriano Repetti Oct 30 '15 at 13:51
  • @AdrianoRepetti `char ch=getchar(); puts("You pressed enter and learned how getchar works");` Compile & run. – Lundin Oct 30 '15 at 13:54
  • Try a new post that contains what you have tried. If the task is daunting -break into smaller programs and work and solve the individual pieces. Certainly you can post some I/O code. – chux - Reinstate Monica Oct 30 '15 at 14:02
  • @Lunding I'm surprised you talk about _standard_ and you assume stdin (not getchar()!) is line buffered... – Adriano Repetti Oct 30 '15 at 14:20

3 Answers3

2

This isn't possible with standard C functions, as they all wait for an end of line/file indicator before returning. The user would have to press enter after every letter they type.

Also, there is no way with standard C to relocate the position in the console where you will be writing the warning.

Since this is a beginner programming course, it would seem that your teacher is simply looking for a non-standard console function. If your teacher is a dinosaur of some sort, the solution is to use the functions from conio.h. Otherwise if your teacher is decent, the solution is to use Ncurses or the Windows API console functions, or a similar modern platform-dependant solution.

Lundin
  • 195,001
  • 40
  • 254
  • 396
2

First of all this is C++ not C because we don't have something called namespace in C. std is a nampesapce. So we don't have std::flush in C as explained in comments and you won't need it.

I just tell you what to do ( I wont write the code! ) :

You haven't told the platform you 're coding on Assuming its windows

  1. You should use a function called getche(). It gets a character from user

  2. Declare a boolean variable that saves the state of brackets and angular brackets.

  3. Put getche() function in a loop and as you encountered '(' or '<' set the variable false.

  4. If you encountered a closing bracker ( I mean ')' or '>' ) set that variable true.

  5. At the end of the application check the variable state. If it was true it means no error was encounterd, otherwise the user had an error.

At last I have put an example for you

Example :

typedef int boolean; /* Just ignore it. Just know it declares boolean type. */ boolean brastate, angbrackstate; char ch = getche(); while(ch != EOF) { if( ch == '<') { angbrackstate = false; ... /* Do the remaining yourself as you like challenges!!! */

Good luck!

0

Just an idea:

  1. Use a text editor that automatically flushes data at regular intervals.

  2. This is a bad idea but you may have to use a infinite loop for checking all data till EOF (End Of File) at regular intervals.(you may use getchar())

Still it mostly depends on how a OS handles a file rwx

  • getchar is a disaster for this exercise –  Oct 30 '15 at 13:12
  • coz as you enter a character you should press Return –  Oct 30 '15 at 13:13
  • flush also isn't good –  Oct 30 '15 at 13:13
  • @Ehsan there is no need to press Return for getchar() it simply gets char from buffer and i was talking about text editor auto save function not c flush() function. –  Oct 31 '15 at 03:23