Pointers not allowed to used iostream

  • Thread starter Thread starter ZakAttk1
  • Start date Start date
  • Tags Tags
    Pointers
Click For Summary

Discussion Overview

The discussion revolves around the challenge of printing a string infinitely to the console without using the iostream library in C++. Participants explore various approaches to achieve this, focusing on pointer manipulation and loop structures.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares an initial code snippet using a pointer to print characters from a string in an infinite loop.
  • Another participant warns that incrementing the pointer indefinitely will lead to accessing memory beyond the string's end, potentially causing a crash.
  • A participant suggests marking the end of the string with a null byte but questions its relevance.
  • Another participant proposes using a condition in the while loop to check for the null terminator instead of relying on an undefined "true".
  • A later reply suggests implementing two nested while loops: one for printing the string and another for resetting the pointer to the beginning after reaching the end of the string.

Areas of Agreement / Disagreement

Participants express differing views on how to handle the end of the string and the structure of the loops, indicating that multiple competing approaches remain without a consensus on the best solution.

Contextual Notes

There are unresolved assumptions regarding the definition of "true" and the handling of the string's end, which may affect the implementation.

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
 

Similar threads

Replies
5
Views
2K
Replies
7
Views
4K
Replies
3
Views
1K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
4
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
12
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K