18640

Scanf missed line

Question:

I wrote a test program which should take in a 3x3 matrix of characters and output the entered matrix. However, I have to enter 4 lines in order for the program to produce the corresponding matrix. I have looked up problems on the scanf function, but none of the solutions I tried seemed to work...Could you help me out with this?

My code:

#include <stdio.h> #include <stdlib.h> int main(void) { char a[3][3]; int i,j; for(i=0;i<3;++i) { for(j=0;j<3;++j) { scanf("%c",&a[i][j]); } scanf("\n"); } for(i=0;i<3;++i) { for(j=0;j<3;++j) { printf("%c",a[i][j]); } printf("\n"); } system("PAUSE"); return(0); }

Answer1:

scanf("%c",...) get the whitespaces and the \n. You can solve it in many ways:

If you read like a b c

for(i=0;i<3;++i) { for(j=0;j<3;++j) { scanf("%c",&a[i][j]); cin.get(); //Get the spaces after each character and the \n at the end of each line } }

or you can simple use cin (read char/string inputs with scanf is always a problem)

for(i=0;i<3;++i) { for(j=0;j<3;++j) { cin >> a[i][j]; } }

if you are reading like abc, you only have to substitute your scanf("\n") for a cin.get()

Answer2:

@João Menighin's answer surely works. If you want to avoid c++, this would work:

for(i=0;i<3;++i) { for(j=0;j<3;++j) { scanf(" %c",&a[i][j]); } }

Although it would ignore ALL whitespace: both abc and a b c would be interpreted to be equivalent.

Answer3:

try adding a white space in your scanf right after the "

scanf(" %c",&a[i][j]);

I had the same problem in a two-dimension matrix and it worked for me. I have no idea why though!!! I just spent 1 hour in front of my laptop trying different things...

Answer4:

Have tried your and IT WORKED. Although, I did make a few changes per comments:

#include <stdio.h> // added, but that shouldn't matter main() { char a[3][3]; int i,j; for(i=0;i<3;++i) { for(j=0;j<3;++j) { scanf("%c",&a[i][j]); } //scanf("\n"); // not necessary, see below } for(i=0;i<3;++i) { for(j=0;j<3;++j) { printf(" %c",a[i][j]); } printf("\n"); } return(0); }

Compiled and ran this code on Eclipse/Microsoft C Compiler and entered series of characters followed by enter.

abcdefghi a b c d e f g h i

The point of confusion might be that scanf pulls the data from a console buffer. Typically, (although you can work around this) that buffer is returned to your program when you press enter. Also, the format specifier of %c also accepts blanks. Thus, I tried a second run with the following input and output.

a b c d e a b c d e

You can tell the spaces were read and stored as well as the letters.

Hope this helps.

Recommend

  • .gitignore not ignoring filenames with spaces in them on `git status`
  • SQLCipher iOS OpenSSL Build error: line 66: ./config: No such file or directory
  • Vim get length of matched line
  • Check precision to nth decimal place?
  • Spinner in Chrome, IE9 does not become visible during synchronous ajax get request
  • Windows batch string manipulation in loop
  • String encoding problem on PdoStatement->bindParam()?
  • Why cout is producing no output on Code Blocks?
  • Create function that can pass a parameter without making a new component
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How can we prepend rows to a react native list-view?
  • Tamper-proof configuration files in .NET?
  • Appending Character to Character Array In C
  • AES padding and writing the ciphertext to a disk file
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • Validaiting emails with Net.Mail MailAddress
  • Javascript + PHP Encryption with pidCrypt
  • Convert array of 8 bytes to signed long in C++
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • ORA-29908: missing primary invocation for ancillary operator
  • Akka Routing: Reply's send to router ends up as dead letters
  • WOWZA + RTMP + HTML5 Playback?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Proper folder structure for lots of source files
  • SQL merge duplicate rows and join values that are different
  • How does Linux kernel interrupt the application?
  • LevelDB C iterator
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Can't mass-assign protected attributes when import data from csv file
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?