Pointers not allowed to used iostream

  • Thread starter Thread starter ZakAttk1
  • Start date Start date
  • Tags Tags
    Pointers
AI Thread Summary
To print a saying infinitely to the console without using iostream, the discussion emphasizes using the putc function within an infinite loop. The initial code attempts to print characters from a string but only outputs the first two characters due to not handling the end of the string correctly. To resolve this, it's crucial to detect the null terminator at the end of the string, which indicates when to stop printing and reset the pointer to the beginning of the string. The recommended approach involves using a nested loop: the outer loop runs indefinitely, while the inner loop prints each character until it reaches the null terminator, at which point it resets the pointer to the start of the string. This structure ensures continuous printing without accessing invalid memory.
ZakAttk1
Messages
7
Reaction score
0
I want to infinitely print a saying to the console but I am not allowed to used iostream.

I am using the function putc. I understand I need to use an infinite loop. So far I have:

int main()
{
char *word;
word = &SAYING[0];

while(true)
{
putc(*word, stdout);
word ++;
putc(*word, stdout);


}


return 0;


However, it only prints the first 2 characters. I need to somehow write some code that automatically prints the saying without having to repeat:

word ++;
putc(*word, stdout);



any advice on how to get started on the rest?
Thanks.
 
Technology news on Phys.org


If you just increment the "word" pointer forever, you'll go past the end of the string and begin printing out random garbage. Your program will eventually crash.

You need to detect that you've reached the end of the string (it will end with a null byte), and go back to the beginning.

- Warren
 


So to mark the end of the string I'll have to do something with *word = '\0'.
Is that relevant?
 


I'm guessing that "true" isn't defined, and you compiler is defaulting it to 0, which keeps the while loop from running. Try while(1), or better still while(*word != 0), and only use one instance of putc(*word), word++.
 


If you want to print the sentence forever, you will need two while loops, one that repeatedly prints the same line, another detects the end of the sentence and go back to print the beginning.
The pseudocode looks like the following:
char saying[]="...";
char *ptr;
while(true)
{
ptr=saying; // starts printing line
while(*ptr!=end-of-string){putc(...); ptr++;}
} // end of line, go back to pring another line
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top