Thread: reading and printing in a specific format strings of characters and integers

  1. #1
    Registered User
    Join Date
    Feb 2015
    Iwakuni, Japan

    reading and printing in a specific format strings of characters and integers

    Hello all.
    I have been skimming and searching but dont know how to word the search just right to find what I need to know. I have written simple stuff with the help of tutorials like weight conversion or loops counting up to a set number. Nothing amazing. The other day I found out how to input and print a string rather than a single text character which i though was bad ass. I even got it to read multiple strings on a single line and found a way to read multiple lines. I can even format it to read both integers and characters on the same line as long as I know the predefined format.

    On to my quesation... How do I read multiple lines with both carecters and integers. for instance:
    nissan 1996
    toyota 1998
    or more comples like
    nissan gtr 1996
    toyota markii 1998

    I want to use
    int year;
    char make[10]; maybe need to use char make[10][10]; for an array i would guess.
    char model[10]; optional for the extra data

    but reproduce what i read in a different order. say...
    1996 nissan
    1998 toyota
    vice the original format.

    this is what I have tried.
    scanf("%s %s", &make,&year);
    //The way I seen to read multiple lines was on here
    scanf("%[^/t]", %make);
    But this wont let me separate the two into two differnet definded data types. Let alone use printf to display them in reverse order. Any pointers? A short explination would help just as much as the code.
    Last edited by Brand_X; 02-23-2015 at 10:53 AM.

  2. #2
    Registered User
    Join Date
    Dec 2012
    although I skipped data checking, this had worked for me!!!

    scanf with two different data types!

    #include <stdio.h>
    int main (void)
     /* variable definition: */
     int year;
     char name[10];
     printf ("Enter a name and a number \n ");
     scanf("%s %d",&name,&year);
     printf ("You typed %s and %d \n ",name,year);
     return 0;
    simple, and crude
    but would be the same concept for an array also!

    as long as your data types, match what your asking for in scanf there should be no problem!

    also no prob with switching the output!
     printf ("You typed %d and %s \n ",year,name);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing wide characters to strings...
    By davo666 in forum C Programming
    Replies: 1
    Last Post: 02-21-2009, 12:56 AM
  2. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 09:53 AM
  3. Replies: 2
    Last Post: 02-11-2008, 03:49 AM
  4. Replies: 6
  5. fread - reading strings and integers.
    By Vber in forum C Programming
    Replies: 1