-
help please!!!!!
:confused: :confused: :confused:
I am only a beginner to C++, but i have to write code to input a poem from the user and output it backwards (line by line). Here is my attempt, can you set me in the right direction to get it working. Any help much appreciated.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int getline(char line[],int max);
int main(void)
{
int nch = 0;
int c;
max = max - 1;
while((c = getchar*()) !=EOF)
{
if(c=='\n')
break;
if(nch<max)
{
line[nch] = c;
nch = nch + 1;
}
}
if(c==EOF&& nch ==0)
return EOF;
line[nch] = '\0';
return nch;
}
main()
{
char *temp_line_ptr;
char *line_ptr;
char **poem_ptr;
char **temp_poem_ptr;
printf("Enter a line of a poem\n",nch);
char *temp_line_ptr = nch;
len = malloc(strlen(*temp_line_ptr)+1);
strcpy(line_ptr,temp_line_ptr);
line_ptr = malloc(sizeof(CHAR)*len);
if(line_ptr == NULL)
{
printf("out of memory\n");
return
}
poem_ptr = malloc(sizeof(char*))
*poem_ptr = line_ptr;
while(getline(line,CHAR) != EOF)
{
*line_ptr = &temp_line_ptr;
**temp_poem_ptr = &poem_ptr;
}
temp_poem_ptr = realloc(poem_ptr,sizeof(char*));
if(temp_poem_ptr !=NULL)
{
line_ptr = temp_poem _ptr;
}
else
{
printf("out of memory\n");
}
*poem_ptr = line_ptr;
printf("\n,line_ptr)
free(line_ptr)
line_ptr = NULL;
}
-
Use code tags, read the rules, and use meaningful topic names.
-
Your code is messed up! You have two mains?
Do you understand this? This doesn't look like a beginners
program.
-
Please dont double post.
Also, use code tags
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int getline(char line[],int max);
int main(void)
{
int nch = 0;
int c;
max = max - 1;
while((c = getchar*()) !=EOF)
{
if(c=='\n')
break;
if(nch<max)
{
line[nch] = c;
nch = nch + 1;
}
}
if(c==EOF&& nch ==0)
return EOF;
line[nch] = '\0';
return nch;
}
main()
{
char *temp_line_ptr;
char *line_ptr;
char **poem_ptr;
char **temp_poem_ptr;
printf("Enter a line of a poem\n",nch);
char *temp_line_ptr = nch;
len = malloc(strlen(*temp_line_ptr)+1);
strcpy(line_ptr,temp_line_ptr);
line_ptr = malloc(sizeof(CHAR)*len);
if(line_ptr == NULL)
{
printf("out of memory\n");
return
}
poem_ptr = malloc(sizeof(char*))
*poem_ptr = line_ptr;
while(getline(line,CHAR) != EOF)
{
*line_ptr = &temp_line_ptr;
**temp_poem_ptr = &poem_ptr;
}
temp_poem_ptr = realloc(poem_ptr,sizeof(char*));
if(temp_poem_ptr !=NULL)
{
line_ptr = temp_poem _ptr;
}
else
{
printf("out of memory\n");
}
*poem_ptr = line_ptr;
printf("\n,line_ptr)
free(line_ptr)
line_ptr = NULL;
}
Your code looks pretty messed up.
You declare getline(..) at the start of the program but you never actually make the method.
You however have two main methods.
You should really try doing this in small steps.
-
And as far as I know, your code is C and not C++.
-
~
Rewrite your code in C++... I mean, you can really shorten your stuff and make it easier to maintain using C++ std libraries like std::string, std::vector, std::ifstream, std::cout...