• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

C Homework: Reading and printing lines of strings

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem figuring out how to print n lines of strings (not just words). I managed to do that after every input line.
But, what I want is to print the below the inputs. For example,

n=3
Input: 1. line: A A A
2. line: B B B
3. line: C C C

Output: 1. line: A A A
2. line: B B B
3. line: C C C


What I am getting is

1. line: A A A
A A A
...


Here is my code (I can't use <string.h> library):



Thanks for replies.

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, what is the program supposed to do?
I can see the replacement for strlen, and can see how that works.
What do the scanf calls in lines 15, 19 and 33 mean? Are you supposed to enter something there? Why is there no prompt printed? You don't need to cram your code so close together. Why does your read procedure have void return type? Why not return some sort of char* or char**?
 
Nemanja Grubor
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program should print n lines of strings after they are entered by user. That is not properly in my code because it prints after every input.

I am using read() function because I can't use <string.h> library.
In function form() I have read the value for number of lines (which is defined as a pointer *pn).

Could you show how to write a function for printing?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my C is EXTREMLY rusty...but you're read method seems to reach in string then print it. you need to read in several strings and save them all in (perhaps) an array.

Then, once you are done getting input, you need to loop through that array and print each one. There's nothing magic there. You already have most of the pieces. You just need to re-arrange them a bit.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic