Pointers not allowed to used iostream

  • Thread starter ZakAttk1
  • Start date
  • Tags
    Pointers
In summary, the conversation discusses the use of an infinite loop to print a saying to the console using the putc function instead of iostream. The main issue is that the program only prints the first two characters and does not continue. The solution is to detect the end of the string and go back to the beginning to continuously print the saying using two nested while loops.
  • #1
ZakAttk1
7
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
  • #2


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
 
  • #3


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


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++.
 
  • #5


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
 

1. What are pointers not allowed to be used for in iostream?

Pointers are not allowed to be used for input/output operations in iostream. This means that you cannot use pointers to read or write data from/to a file or console.

2. Why are pointers not allowed in iostream?

Pointers are not allowed in iostream because they can create security vulnerabilities and lead to unexpected behavior. Additionally, iostream already has functions and operators that perform the necessary tasks without the need for pointers.

3. Can I use pointers in other C++ libraries besides iostream?

Yes, you can use pointers in other C++ libraries as long as they are designed to handle pointers and do not pose any security threats. However, it is generally recommended to avoid using pointers whenever possible to minimize the risk of errors and bugs.

4. How can I perform input/output operations without using pointers in iostream?

Iostream offers alternative functions and operators that allow you to perform input/output operations without the use of pointers. For example, you can use the extraction and insertion operators (>> and <<) to read and write data from/to a file or console.

5. Are there any alternatives to using pointers in iostream?

Yes, there are alternatives to using pointers in iostream. You can use references, which are similar to pointers but offer more safety and convenience. You can also use objects and classes to manage input/output operations in a more structured and organized manner.

Similar threads

  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
5
Views
881
  • Programming and Computer Science
Replies
3
Views
726
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top