how is this possible

closed account (9G3v5Di1)
Write a program that will display the words “This is it!” from a variablewithout assigning any characters to the variable. You cannot use cout << “This is it!” << endl; or any variants of it.

how is it possible without using printf?
Hello goldwinbryanr,

Because it is a C++ assignment not a C assignment.
That's a really, really vague description of the problem. Are you sure you've told us completely and precisely what the problem is you've been told to solve?

From what you've written, I'm wondering if a solution is to use the numerical values of the characters you want to put into the string variable, rather than the actual characters.
closed account (9G3v5Di1)
that is exactly the problem written in my laboratory exercise..
Here's something that outputs "Th".

1
2
3
4
5
6
7
8
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> values{84, 104};
    for (auto& c : values) { std::cout << (char)c;}
}


The rest is up to you. You'll need this: http://www.asciitable.com/

Questions such as you have rarely happen in a vacuum. If these are tasks being set as part of some kind of education, then whatever you have been learning about before the task is likely to be relevant to how you're meant to do it.
Last edited on
@goldwinbryanr,

You can link your different threads:
http://www.cplusplus.com/forum/beginner/243195/#msg1078585
How about calling a function that returns a string "This is it!". You do not use a variable, for the most part, and you do not put the words directly in the "cout" statement.

Andy
@Handy Andy The question the OP posted explicitly states that they much use a variable. They just assign any characters to the variable.

Using the numerical values instead would seem a valid way to do this.
Is this allowed?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
	char a[] = "Uijt!jt!ju\"";

	for (int i = 0; i < 11; i++) {
		a[i]--;
		cout << a[i];
	}

	cin.get();
	return 0;

}
@MikeyBoy,

It was a thought.

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>
#include <iterator>

struct this_is_it
{
    this_is_it() // (default) constructor: (default) initialises the object
    {
        for( auto p = ++std::rbegin(cstr) ; p < std::rend(cstr) ; ++++p )
            std::putchar(*p) ;
    }

    // static constexpr member
    // note that the initialiser is used for compile-time initialisation (not assignment)
    // also note that the const qualified characters can't be assigned to
    static constexpr char cstr[] = "\n\n!!ttii  ssii  ssiihhtt" ;
};

int main()
{
    // declare a variable of type this_is_it
    const this_is_it this_really_is_it ; // the object is default-initialised
}

http://coliru.stacked-crooked.com/a/35b7db1291ed48f3
display the words “This is it!” from a variablewithout assigning any characters to the variable

I may be naive, but I suspect what's required is just a trivial trick like:
1
2
3
4
5
6
7
8
#include <iostream>
 
int main()
{
    constexpr char str[] { "So: This is it!" };
    const char * const p = &str[4];
    std::cout << p << '\n';
}

At this point, it seems like we're just trying to read the mind of a teacher who couldn't be bothered to clearly define the problem s/he wants students to solve.
and then there is:

unsigned long long hax[2] = {2338328219631577172ull, 2192489};
cout << (char*) hax << endl;

constants will be different for other endian setups, but this worked for mine.
closed account (9G3v5Di1)
how did you do that @jonnin?
an integer is just some bytes. On my system a long long is 8 bytes, so it can hold 8 letters.

try this...
char c[9] = "This is "; //this is a C string, and it has an extra zero sentinel character. so 8 bytes of letters and 1 byte extra = 9. This_is_ (_ is space) is 8 letters.


unsigned long long *x = (unsigned long long *)c;
cout << x; //this is the first number.

the second number is the same, except do this..
char c2[9] = "it!\0\0\0\0\0" to get the value. the \0s after the first should not matter.

This is probably not what your teacher wants. Its the letter of the law, but not the intent, so to speak... I probably would have turned it in in college... by college I had already coded for ~2 years and was bored with the intro classes and drove my poor professors nuts.
Last edited on
Topic archived. No new replies allowed.