Multiline with Cstring library HELP

closed account (ivDwAqkS)
does anybody knows how to write multilines with cstring library, id like to know for example do a program that in wich you can write many lines of text but if you press enter i dont want it close the program id like to still running the program until you write "0" or "QUIT" and out put the same text as you wrote.
Last edited on
closed account (ivDwAqkS)
need help
What have you tried so far?

Also, why do you want to use C-style strings? You have to continually create a larger buffer and copy the old string into the new buffer; it's a real pain and easy to get wrong.
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <string.h>

int main()
{
    static char cstr[1024*1024] ;
    puts( "lines you enter are echoed. enter QUIT to quit" ) ;

    while( fgets( cstr, sizeof(cstr), stdin ) && strcmp( cstr, "QUIT\n" ) )
        puts(cstr) ;
}
Last edited on
closed account (ivDwAqkS)
LB there is not way to do it with just one character? for example like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstring>
using namespace std;

int main(){
    chat string[999+1];
    
    cout << "Enter a text(press 0 to quit)";
    /*I wanted to enter a text in wich still writing the same string while pressing enter
     even though it doesn't work, 
     when I press enter the program just close and gives me the first line*/
    while(true){
        cin.getline(string, 999);
        if(strcmp(string, "0"))
            break;
    }
   cout << string << endl;
    return 0;
}



Last edited on
strcmp returns 0 if the two strings are equal.
You're comparing the two strings and breaking if they're not equal.

In any case, the output of your program will be the last string you entered (since you overwrite your string with the new input each time).
If you don't want that, then C strings are really not the best way to go, since you'd have to allocate the strings yourself and resize them from time to time....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <string.h>

void echo()
{
	const char *exitvals[] = 
	{
		"0", "QUIT"
	};
	
	char buf[] = {"\0\0\0\0\0"}, *ptr = buf;
	
	int c;
	for (c = getchar(); c != EOF; c = getchar())
	{
		ptr = &buf[0];
		while(c != '\n' && ptr != &buf[5])
		{
			*ptr++ = c;
			c = getchar();
		}
		
		if (strcmp(buf, exitvals[0]) && strcmp(buf, exitvals[1]))
		{
			for (ptr = &buf[0]; *ptr; ptr++)
			{
				putchar(*ptr);
				*ptr = '\0';
			}
		}
		else break;
		
		while (c != '\n')
		{
			putchar(c);
			c = getchar();
		}
		
		putchar(c);
	}
}
Last edited on
closed account (ivDwAqkS)
Thank you , i just wanted.to.know that answer, i know.jow to.do it with.string library, but just.wanted.to make.sure.if.i could.do it with cstring library, but.now.i know.i cant.do it.with a.simple.character, only if.you size.it.with.an array
Topic archived. No new replies allowed.