Assignment #1

Fa42c77e5adb23f3737a6845255c8a53
0
noctrnl13 101 Apr 21, 2011 at 02:35

I’m sorry for bothering you programming gurus with such a simple question, I’m fairly new to the programming scene, although I want to get into it heavily and plan too. (study hours already set out) With all this excitement I got stuck at the first assignment on the C++ tutorial.

This is my task:
Create an executable that prints your name 10 times, then waits for a key, then prints ‘IGAD rules!’ 10 times, then waits for a key again, then exits.

I have no idea how to accomplish this or where to start. Any help would be great, also explain if you could I’m not really looking for just the answers.

Thanks a bunch.

11 Replies

Please log in or register to post a reply.

Fa42c77e5adb23f3737a6845255c8a53
0
noctrnl13 101 Apr 21, 2011 at 03:09

I’m new to C++, in python you could just use an operand to “multiply” the statement. It’s my understanding strings do not work the same way in C++?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Apr 21, 2011 at 03:34

Look at the Hello World program listed in the lesson. It’s got a line of code that prints the words “Hello World” to the screen, and then another line that waits for you to press a key. It should be fairly obvious which is which. ;) You need to adapt this to do the task in the homework. You’ll have multiple copies of each of those lines of code, and of course you’ll replace “Hello World” with your own text.

Fa42c77e5adb23f3737a6845255c8a53
0
noctrnl13 101 Apr 21, 2011 at 03:50

Thank you, I can’t believe I didn’t get this considering how simple it was.
This is what my code looks like:
#include “stdio.h”

void main ()
{
printf(“Yjaden Wood!\n”);
printf(“Yjaden Wood!\n”);
printf(“Yjaden Wood!\n”);
printf(“Yjaden Wood!\n”);
getchar();
printf(“IGAD Rules!\n”);
printf(“IGAD Rules!\n”);
printf(“IGAD Rules!\n”);
printf(“IGAD Rules!\n”);
getchar();
}

Fa42c77e5adb23f3737a6845255c8a53
0
noctrnl13 101 Apr 21, 2011 at 03:52

Also I just noticed it says you work at SuckerPunch that’s awesome. Great game dev.

820ce9018b365a6aeba6e23847f17eda
0
geon 101 Apr 21, 2011 at 09:20

Yup, that would work, but it’s more C than C++. Don’t confuse the two, or you’ll never write decent code in either.

In C++, you would usually print with the cout object:

cout << "some string" << "another string" << 1234 << "Yes, that was an integer";

Another important principle of programming is to Don’t Repeat Yourself, (DRY). Use a loop to print the same string many times instead of repeating the same line of code. That will make your code much more flexible, readable and maintainable.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Apr 21, 2011 at 16:16

geon, this is the first lesson of the tutorial; he hasn’t gotten to loops yet. ;)

As for printf vs cout…I have to say, I honestly prefer printf. The cout approach of overloading some random operator just seems ugly to me.

6837d514b487de395be51432d9cdd078
0
TheNut 179 Apr 21, 2011 at 17:04

@Reedbeta

I honestly prefer printf

+1
@Reedbeta

geon, this is the first lesson of the tutorial; he hasn’t gotten to loops yet

Let’s teach him how to do it using template metaprogramming and see what happens next :lol:

Fa42c77e5adb23f3737a6845255c8a53
0
noctrnl13 101 Apr 22, 2011 at 01:59

I understand the conditional statements….somewhat, I have to agree with preferring printf. I’m going to try to use a loop statement to see if I can reprint my original statement. I figured there has to be a more clean way of accomplishing what I just did thanks guy for the comments.

Fd80f81596aa1cf809ceb1c2077e190b
0
rouncer 103 Apr 22, 2011 at 05:00

are you having fun so far? if you are its a good sign youll take it to the finish.

Fa42c77e5adb23f3737a6845255c8a53
0
noctrnl13 101 Apr 23, 2011 at 21:41

I am, everyone downs programming as boring but I find great enjoyment in it. I know that’s a strong statement for a beginner but I can see myself doing this for a long time.

620d64360f87e70ea3f30106a68989a8
0
Rofar 101 Apr 25, 2011 at 15:47

@Reedbeta

As for printf vs cout…I have to say, I honestly prefer printf. The cout approach of overloading some random operator just seems ugly to me.

I’ve never used cout in my code. printf works great. Never felt the need to use cout.