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
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top