reading 2 seperate inputs, possible overwriting?

This is a discussion on reading 2 seperate inputs, possible overwriting? within the C Programming forums, part of the General Programming Boards category; What i want to do is to input 30 characters at the standard input, then covert them to uppercase. This ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01:21 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21