Thread: reading 2 seperate inputs, possible overwriting?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    35

    reading 2 seperate inputs, possible overwriting?

    What i want to do is to input 30 characters at the standard input, then covert them to uppercase. This is hopefully leading somewhere, so i only want to read 15 characters at a time, then convert those to uppercase, then do the second 15?

    here's the code i have so far but have the slight problem, where i over write things, and because I'm new to C i cant quite get my read round it.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main( void )
    {
    char text[64];
    int z=0, counter=0;
    
    while(++counter<=2)
    {
    scanf("%15c", &text);
    while(text[z]>0)
    {
    text[z]=toupper(text[z]);
    printf("%c", text[z]);
    z++;
    }
    
    }
    }
    Thank you for reading this, i hope you can help me

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Bear in mind that %15c will not append a \0 to whatever string is read, thus making your inner loop run as expected.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    I'm confused :s why does it only work once? and not for 2 15 character inputs

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well the other thing you don't do is reset z back to 0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating seperate functions for inputs, outputs and calculations?
    By toby2604HASSELB in forum C Programming
    Replies: 3
    Last Post: 10-21-2010, 02:26 AM
  2. Array (seperate inputs by comma)
    By chuckwag0n in forum C++ Programming
    Replies: 7
    Last Post: 04-03-2009, 01:10 PM
  3. Array (seperate inputs by comma)
    By chuckwag0n in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 01:01 PM
  4. Problem in reading inputs
    By Bargi in forum C Programming
    Replies: 15
    Last Post: 06-18-2008, 09:28 AM
  5. need help for reading two keyboard inputs
    By Diablo02 in forum C# Programming
    Replies: 12
    Last Post: 12-06-2007, 02:21 PM

Tags for this Thread