improve it at will.

The purpose of this code is to use an array of pointers and one function that returns a pointer. Play with it at will.

As is it outputs: this is a sentence!

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
#include <iostream>
#include <stdlib.h>
#include <cstdio>


using namespace std;

char *getme(char *p[], char ch);

int main(void)
{

    char *pt, ch = '1';

    char *str[]= {"this ", "is a ", "sentence! "};
    while(ch <= '3' )
    {
        pt=getme(str,ch);
        cout << pt;
        //printf("%s",pt);
        ch+=1;
    }
    return 0;

}
char *getme( char *p[], char ch)
{
    if(ch == '1')
        return p[0];
    else if (ch == '2')
        return p[1];
    else if (ch == '3')
        return p[2];
}
I don't understand - did you intentionally write this code to be bad, and you want people to make it less bad?
I don't understand - did you intentionally write this code to be bad, and you want people to make it less bad?

This?


Same result in 10 lines without the pointless function.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string Parts[] = { "this ", "is a ", "sentence! " };
	for (int i = 0; i < 3; i++)
		cout << Parts[i];
	return 0;
}
Last edited on
Here is an improvement

1
2
3
4
5
#include<iostream>
int main(){
std::cout << "this is a sentence!";
return 0;
}
Last edited on
I smell a troll.
char *str[]= {"this", "is a ", "sentence! "};
how can i know the size of str[2] ??
1
2
auto size1 = std::strlen(str[2]);
auto size2 = std::string(str[2]).length();
switching to ANSI C:
int main() {return 0/printf("this is a sentence!");}
Any program in C/C++ can be represented as one very long line, bar preprocessor statements
Topic archived. No new replies allowed.