Jump to content


Assignment #1


11 replies to this topic

#1 noctrnl13

    New Member

  • Members
  • Pip
  • 8 posts

Posted 21 April 2011 - 02:35 AM

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.

#2 noctrnl13

    New Member

  • Members
  • Pip
  • 8 posts

Posted 21 April 2011 - 03:09 AM

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++?

#3 Reedbeta

    DevMaster Staff

  • Administrators
  • 5344 posts
  • LocationSanta Clara, CA

Posted 21 April 2011 - 03:34 AM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#4 noctrnl13

    New Member

  • Members
  • Pip
  • 8 posts

Posted 21 April 2011 - 03:50 AM

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();
}

#5 noctrnl13

    New Member

  • Members
  • Pip
  • 8 posts

Posted 21 April 2011 - 03:52 AM

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

#6 geon

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 21 April 2011 - 09:20 AM

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.

#7 Reedbeta

    DevMaster Staff

  • Administrators
  • 5344 posts
  • LocationSanta Clara, CA

Posted 21 April 2011 - 04:16 PM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#8 TheNut

    Senior Member

  • Moderators
  • 1719 posts
  • LocationCyberspace

Posted 21 April 2011 - 05:04 PM

Reedbeta said:

I honestly prefer printf
+1

Reedbeta said:

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:
http://www.nutty.ca - Being a nut has its advantages.

#9 noctrnl13

    New Member

  • Members
  • Pip
  • 8 posts

Posted 22 April 2011 - 01:59 AM

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.

#10 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2759 posts

Posted 22 April 2011 - 05:00 AM

are you having fun so far? if you are its a good sign youll take it to the finish.
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

#11 noctrnl13

    New Member

  • Members
  • Pip
  • 8 posts

Posted 23 April 2011 - 09:41 PM

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.

#12 Rofar

    Member

  • Members
  • PipPip
  • 99 posts

Posted 25 April 2011 - 03:47 PM

Reedbeta said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users