Search Results

Search found 278 results on 12 pages for 'scanf'.

Page 1/12 | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Scanf with Signals

    - by jreid42
    I have a signal that blocks SIGINT and basically says "Sorry, you can't quit.\n" The issue is this can occur during a scanf. When this occurs during a scanf, scanf takes in the printf as input. How can I do a printf that will cause scanf to basically hit the enter key automatically. I don't care that I am getting bad input. I just want to programatically finish that scanf with a printf or something else. Process: scanf("get stuff") - User is able to enter stuff in. - SIGINT occurs and goes to my handler. - Handler says "Blah blah blah" to stdout. - Scanf has taken this blah blah blah and is waiting for more input. How do I make it so that when I return scanf is finished (don't care what it has gathered I just want it to continue without user help).

    Read the article

  • How to read in a negative double with scanf() in C

    - by rize
    I'm learning basics of C and writing a simple first order equation solver. I want the input to be exactly ax+b=c or ax-b=c, where a, b, c are double type. I'm employing scanf() to read in user input and to check if it's of the correct form. However, if I enter a negative double, -4.6 say, as the "a" in the equation, scanf() won't read the a,b,c correctly. I'm using %lf inside scanf(). How do I read a negative double, then? Many thanks. My code: if (scanf("%lfx+%lf=%lf", &a, &b, &c)) more code If I use as the input "-6.2x+3.4=-5.9", the value 3.4 will be assinged to variable a, while b and c remain as they were and "more code" is run.

    Read the article

  • scanf() (C Language ) confused me

    - by Nano HE
    Hello. When do I need to insert/don't insert & for scanf() in C? Thank you. int main() { char s1[81], s2[81], s3[81]; scanf("%s%s%s", s1, s2, s3); // If replace scanf() with the expression below, it works too. // scanf("%s%s%s", &s1, &s2, &s3); printf("\ns1 = %s\ns2 = %s\ns3 = %s", s1, s2, s3); return 0; } //programming is fun // //s1 = programming //s2 = is //s3 = fun

    Read the article

  • Newb Question: scanf() in C

    - by riemannliness
    So I started learning C today, and as an exercise i was told to write a program that asks the user for numbers until they type a 0, then adds the even ones and the odd ones together. Here is is (don't laugh at my bad style): #include <stdio.h>; int main() { int esum = 0, osum = 0; int n, mod; puts("Please enter some numbers, 0 to terminate:"); scanf("%d", &n); while (n != 0) { mod = n % 2; switch(mod) { case 0: esum += n; break; case 1: osum += n; } scanf("%d", &n); } printf("The sum of evens:%d,\t The sum of odds:%d", esum, osum); return 0; } My question concerns the mechanics of the scanf() function. It seems that when you enter several numbers at once separated by spaces (eg. 1 22 34 2 8), the scanf() function somehow remembers each distinct numbers in the line, and steps through the while loop for each one respectively. Why/how does this happen? Example interaction within command prompt: - Please enter some numbers, 0 to terminate: 42 8 77 23 11 (enter) 0 (enter) - The sum of evens:50, The sum of odds:111 I'm running the program through the command prompt, it's compiled for win32 platforms with visual studio.

    Read the article

  • Scanf fails with bus error

    - by Mikulas Dite
    I'm playing with C and I've run into this error: #include <stdio.h> int main () { char* foo; scanf("%s", foo); printf("entered %s", foo); return 0; } scanf takes pointer, foo is pointer, yet I get bus error. How can I make it work?

    Read the article

  • Input/ Output alternatives for printf/scanf

    - by Nitish
    It may sound strange that knowing a lot about ios and haing some experience in .net, I am a newcomer to C. Somewhere I got this target to find average of n numbers without using printf and scanf. I don't want the code for the program but it will be really helpful if someone can help me with the alternatives to the mentioned functions. Please let me know if code with printf/scanf is required here. Also do let me know if my query stands invalid. Thanks and Regards, Nitish

    Read the article

  • Using an ampersand in scanf()

    - by Rob
    When I compile scanf("%s", &var);, gcc sends back a warning: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’ however when I compile scanf("%s", var);, no warning is applied. Both pieces of code work and the book I am reading specifically says to use the ampersand, but even it doesn't in some of the examples. My question is, should I continue to use the ampersand, even when the book doesn't specify?

    Read the article

  • Getting multiple values with scanf()

    - by Josh Curren
    I am using scanf() to get a set of ints from the user. But I would like the user to supply all 4 ints at once instead of 4 different promps. I know I can get one value by doing: scanf( "%i", &minx); But I would like the user to be able to do something like: Enter Four Ints: 123 234 345 456 Is it possible to do this?

    Read the article

  • Objective-C scanf spaces issue

    - by Rob
    I am learning objective-C and for the life of me can't figure out why this is happening. When the user inputs when the code is: scanf("%c %lf", &operator, &number); For some reason it messes with this code: doQuit = 0; [deskCalc setAccumulator: 0]; while (doQuit == 0) { NSLog(@"Please input an operation and then a number:"); scanf("%c %lf", &operator, &number); switch (operator) { case '+': [deskCalc add: number]; NSLog (@"%lf", [deskCalc accumulator]); break; case '-': [deskCalc subtract: number]; NSLog (@"%lf", [deskCalc accumulator]); break; case '*': case 'x': [deskCalc multiply: number]; NSLog (@"%lf", [deskCalc accumulator]); break; case '/': if (number == 0) NSLog(@"You can't divide by zero."); else [deskCalc divide: number]; NSLog (@"%lf", [deskCalc accumulator]); break; case 'S': [deskCalc setAccumulator: number]; NSLog (@"%lf", [deskCalc accumulator]); break; case 'E': doQuit = 1; break; default: NSLog(@"You did not enter a valid operator."); break; } } When the user inputs for example "E 10" it will exit the loop but it will also print "You did not enter a valid operator." When I change the code to: scanf(" %c %lf", &operator, &number); It all of a sudden doesn't print this last line. What is it about the space before %c that fixes this?

    Read the article

  • scanf segfaults and various other anomalies inside while loop

    - by Shadow
    while(1){ //Command prompt char *command; printf("%s>",current_working_directory); scanf("%s",command);<--seg faults after input has been received. printf("\ncommand:%s\n",command); } I am getting a few different errors and they don't really seem reproducible(except for the segfault at this point .<). This code worked fine about 10 minutes ago, then it infinite looped the printf command and now it seg faults on the line mentioned above. The only thing I changed was scanf("%s",command); to what it currently is. If I change the command variable to be an array it works, obviously this is because the storage is set aside for it. 1) I got prosecuted about telling someone that they needed to malloc a pointer* (But that usually seems to solve the problem such as making it an array) 2) the command I am entering is "magic" 5 characters so there shouldn't be any crazy stack overflow. 3) I am running on mac OSX 10.6 with newest version of xCode(non-OS4) and standard gcc 4) this is how I compile the program: gcc --std=c99 -W sfs.c Just trying to figure out what is going on. Being this is for a school project I am never going to have to see again, I will just code some noob work around that would make my boss cry :) But for afterwards I would love to figure out why this is happening and not just make some fix for it, and if there is some fix for it why that fix works.

    Read the article

  • scanf a byte then print it out?

    - by Sarah
    I've searched around to see if I can find this answer but I can't seem to (please let me know if I'm wrong). I am trying to use scanf to read in a byte, an unsigned int and a char in one .c file and I am trying to access this byte in a different .c file and print it out. (I have already checked to make sure I have included all the appropriate parameters everywhere) But I keep getting errors. The warnings are: database.c: In function ‘addCitizen’: database.c:23:2: warning: format ‘%hhu’ expects argument of type ‘int’, but argument 2 has type ‘byte *’ [-Wformat] database.c:24:2: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat] database.c:25:2: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat] where I'm scanf'ing: // Request loop while (count-- != 0) { while (1){ // Get values from the user int error = scanf ("%79s %hhu %u %c", tname, &tdist, &tyear, &tgender); addCitizen(db, tname, &tdist, &tyear, &tgender); where I'm printing: void addCitizen(Database *db, char *tname, byte *tdist, int *tyear, char *tgender){ //needs to find the right place in memory to put this stuff and then put it there printf("\nName is: %79s\n", tname); printf("District is: %hhu\n", tdist); printf("Year of birth is: %u\n", tyear); printf("Gender is:%c\n", tgender); I'm not sure where I'm going wrong?

    Read the article

  • Is there a way to read a c-string and then an int with a single scanf in C?

    - by Aux
    Hey, I'm trying to get this function to get the following output with the listed input, the "..." is where I'm not sure what to write: void Question8(void) { char sentence[100]; int grade; scanf(….); printf("%s %d", sentence, grade); } Input: My CS Grade is 1000 Output: My CS Grade is 100 However, the kicker is that I need the scanf to read a c-string and then an int with a single scanf command, is this even possible?

    Read the article

  • segmentation fault using scanf

    - by agarrow
    noob question here: I'm trying to write a simple menu interface, but I keep getting a segmentation fault error and I can't figure out why. #include <stdlib.h> #include <stdio.h> int flush(); int add(char *name, char *password, char *type); int delete(char *name); int edit(char *name, char *password, char *type, char *newName, char *newPassword, char *newType); int verify(char *name, char *password); int menu(){ int input; char *name, *password, *type, *newName, *newPassword, *newType; printf("MAIN MENU \n ============\n"); printf("1. ADD\n"); printf("2. DELETE\n"); printf("3. EDIT\n"); printf("4. VERIFY\n"); printf("5. Exit\n"); printf("Selection:"); scanf("%d", &input); flush(); switch (input){ case 1: printf("%s\n", "Enter Name:"); scanf("%s", name); flush(); printf("%s\n", "enter password" ); scanf("%s", password); flush(); printf("%s\n","enter type" ); scanf("%s",type); add(name, password, type); menu(); break; case 2: printf("Enter Name:" ); scanf("%s",name); flush(); delete(name); menu(); break; case 3: printf("Enter Name:\n"); scanf("%s",name); flush(); printf("Enter Password\n"); scanf("%s", password); flush(); printf("enter type:\n"); scanf("%s", type); flush(); printf("enter your new username:\n"); scanf("%s",newName); flush(); printf("enter your new password\n"); scanf("%s", newPassword); flush(); printf("enter your new type\n"); scanf("%s",newType); flush(); edit(name, password, type, newName, newPassword, newType); menu(); break; case 4: printf("Enter Name\n"); scanf("%s",name); flush(); printf("Enter Password\n"); scanf("%s",password); flush(); verify(name, password); menu(); break; case 5: return 0; default: printf("invalid input, please select from the following:\n"); menu(); } return 0; } int flush(){ int ch; while ((ch = getchar()) != EOF && ch != '\n') ; return 0; } I get the segmentation fault after entering two fields, in any menu option

    Read the article

  • Ignore carriage returns in scanf before data.... to keep layout of console based graphics with conio

    - by volting
    I have the misfortune of having use conio.h in vc++ 6 for a college assignment, My problem is that my graphic setup is in the center of the screen... e.g. gotoxy( getcols()/2, getrows()/2); printf("Enter something"); scanf( "%d", &something ); now if someone accidentally hits enter before they enter the "something", then the cursor gets reset to the left of the screen on the next line. Iv tried flushing the keyboard and bios buffers with fflush(stdin) and getchar(), which like I expected didn't work! Any help/ideas would be appreciated, Thanks, V

    Read the article

  • Disadvantages of scanf

    - by karthi-27
    Hi all, I want to know the disadvantage of the scanf.In many of a sites I have read that using scanf will cause buffer overflow some times.What is the reason for that , and is there any other drawbacks with scanf .

    Read the article

  • how to read scanf with spaces

    - by Matias
    I'm having a weird problem i'm trying to read a string from a console with scanf() like this scanf("%[^\n]",string1); but it doesnt read anything. it just skips the entire scanf. I'm trying it in gcc compiler

    Read the article

  • C, reading multiple numbers from single input line (scanf?)

    - by michal
    Hi, I'm writing an app in pure C which expects two line at input: first one, which tells how big an array of int will be, and the second, which contains values, separated by space. So, for the following input 5 1 2 3 4 99 I need to create an array containing {1,2,3,4,99} Now, my question is - what is the fastest (yet easy ;)) way to do so? My problem is "how to read multiple numbers without looping through the whole string checking if it's space or a number" ? Thanks m.

    Read the article

  • getchar does not stop when using scanf

    - by Oz123
    I am have a difficulty understanding getchar(). In the following program getchar works as expected: #include <stdio.h> int main() { printf("Type Enter to continue..."); getchar(); return 0; } However, in the following program, getchar does not create a delay and the program ends: #include <stdio.h> int main() { char command[100]; scanf("%s", command ); printf("Type Enter to continue..."); getchar(); return 0; } I have the following weired workaround, which works, but I understand why: #include <stdio.h> int main() { char command[100]; int i; scanf("%s", command ); printf("Type Enter to continue..."); while ( getchar() != '\n') { i=0; } getchar(); return 0; } So my questions are: 1. What is scanf doing? Why does scanf do this ? 2. Why is my work around working? 3. What is a good way to emulate the following Python code: raw_input("Type Enter to continue")

    Read the article

  • scanf stop reading when eol is seen

    - by gcc
    scanf("%[^\n]\n",A[i]); /*that reads input line by line; example:first line is stored in A[0]*/ -> but I want read each element of line and send to struct fuction until the EOL (end of line) -> explaining: in current line,read one data ,then send to struct funcion to hold,after then ,in for loop, read next data decide it is float then send it to function. when eol is read, then activate next struct. >question is I want write something in scanf such that I stop read when i see eol. can I do for( ; ; ) { scanf("...",Sam); if(Sam=='\n) break; }

    Read the article

  • Program execution stop at scanf???

    - by Mohit Deshpande
    main.c (with all the headers like stdio, stdlib, etc): int main() { int input; while(1) { printf("\n"); printf("\n1. Add new node"); printf("\n2. Delete existing node"); printf("\n3. Print all data"); printf("\n4. Exit"); printf("Enter your option -> "); scanf("%d", &input); string key = ""; string tempKey = ""; string tempValue = ""; Node newNode; Node temp; switch (input) { case 1: printf("\nEnter a key: "); scanf("%s", tempKey); printf("\nEnter a value: "); scanf("%s", tempValue); //execution ternimates here newNode.key = tempKey; newNode.value = tempValue; AddNode(newNode); break; case 2: printf("\nEnter the key of the node: "); scanf("%s", key); temp = GetNode(key); DeleteNode(temp); break; case 3: printf("\n"); PrintAllNodes(); break; case 4: exit(0); break; default: printf("\nWrong option chosen!\n"); break; } } return 0; } storage.h: #ifndef DATABASEIO_H_ #define DATABASEIO_H_ //typedefs typedef char *string; /* * main struct with key, value, * and pointer to next struct * Also typedefs Node and NodePtr */ typedef struct Node { string key; string value; struct Node *next; } Node, *NodePtr; //Function Prototypes void AddNode(Node node); void DeleteNode(Node node); Node GetNode(string key); void PrintAllNodes(); #endif /* DATABASEIO_H_ */ I am using Eclipse CDT, and when I enter 1, then I enter a key. Then the console says . I used gdb and got this error: Program received signal SIGSEGV, Segmentation fault. 0x00177024 in _IO_vfscanf () from /lib/tls/i686/cmov/libc.so.6 Any ideas why?

    Read the article

  • How to search for a string including spaces in Objective C?

    - by AlexCu
    I have a real basic command-line program, in Objective-C, that searches for user inputed information. Unfourtunately, the code will only read the first word in series of words that the user enters. For example, if the user enters in "Apples are great", only "Apples" is kept (and hence searched later on), excluding the "are great" part of the sentence. Here's what I have so far: char enteredQuery [128]; // array 'name' to hold the scanf string NSString *searchQuery; // ending NSString to hold and compare the user inputed data NSLog(@"Enter search query:"); scanf("%s", enteredQuery); //will read the next line searchQuery = [NSString stringWithCString: enteredQuery encoding: NSASCIIStringEncoding]; //converts scanf data into a NSString type I know it's got to do with me using scanf or the character-encoder conversion, but I can't seem to figure it out. Any help in solving the problem is very appreciated! Thanks.

    Read the article

  • Looking for Go equivalent of scanf.

    - by Stephen Hsu
    I'm looking for the Go equivalent of scanf(). I tried with following code: 1 package main 2 3 import ( 4 "scanner" 5 "os" 6 "fmt" 7 ) 8 9 func main() { 10 var s scanner.Scanner 11 s.Init(os.Stdin) 12 s.Mode = scanner.ScanInts 13 tok := s.Scan() 14 for tok != scanner.EOF { 15 fmt.Printf("%d ", tok) 16 tok = s.Scan() 17 } 18 fmt.Println() 19 } I run it with input from a text with a line of integers. But it always output -3 -3 ... And how to scan a line composed of a string and some integers? Changing the mode whenever encounter a new data type? The Package documentation: Package scanner A general-purpose scanner for UTF-8 encoded text. But it seems that the scanner is not for general use. Updated code: func main() { n := scanf() fmt.Println(n) fmt.Println(len(n)) } func scanf() []int { nums := new(vector.IntVector) reader := bufio.NewReader(os.Stdin) str, err := reader.ReadString('\n') for err != os.EOF { fields := strings.Fields(str) for _, f := range fields { i, _ := strconv.Atoi(f) nums.Push(i) } str, err = reader.ReadString('\n') } r := make([]int, nums.Len()) for i := 0; i < nums.Len(); i++ { r[i] = nums.At(i) } return r }

    Read the article

1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >