Translate a simple script from c to c++

i found a neat comic with some c and i want to remake it in c++

Page 1 of 1

14 Replies - 3001 Views - Last Post: 22 February 2009 - 11:29 PM Rate Topic: -----

#1 <binary_glitch>  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 27-November 08

Translate a simple script from c to c++

Post icon  Posted 20 February 2009 - 12:43 AM

Okay I found a comic strip on the web that... well it's of a young student in a class room, and he's standing in front of his teacher with chalk in hand and on the black-board in the backround there is some c program code that writes "I will not throw paper airplanes in class" 500 times (the code has what could be consitered an error, at the end of his string there is no new line marker ie /n) the teacher has a speach bubble containing the words "nice try" I like this cartoon because it's programmer oriented and because there's a lot to think about, like even though it's obviously grade school level and the likliehood of the both the studen and the teacher knowing c is minamal, still it could happen, maybe it's an advanced school for the gifted and this is a programming class, in that case, instead of the obvious, the meaning behind the teacher saying nice try could be, not that he was trying to get out of writing it 500 times, but that his code is ereonious? Whatever but the point is that I find the comic beautifull (artistscly) even the error. So I wanted to see if I could translate it to c++, this (at my new-to-programming skill level) would be a little challenge for myself, and thus a good assingment for my self teaching/learning method. Or so I though.

I will now give you the original code verbadom, then I will give you the code that I came up with. I got errors while compiling, and ran out of time to play before I had to get ready for work so... I'm at work on break now so I don't have my error messages with me but when I get home, sleep and awake, I'll recompile, and post them.

The Original C Code:

#include <stdio.h>
int main(void)
{
   Int count;
   for (count = 1; count<= 500; count++) 
	  printf("I will not throw paper airplanes in class.");
return 0;
} 



My Translation:
#include<iostream>
using namespace std;

int count=0;
int main(void)

{
For (count<=500, count++)
{
cout >>"I will not throw paper airplanes in class";
}
return 0;
}



If you wanna give a stab at some suggestions, or if you see a/some fundamental fault(s) in my code and want to post before I post my compiler errors that's cool, I'll give them a try. Thanx in advance.

P.S.
I'm a newbe so be easy on me. Also I'm self teaching/learning and I just dove into code, I have no idea about structure or how to think abou bigger projects so if that's obvious in my code... well feel free to educate me, but keep in mind you'd be educating me, not lecturing me on something I should already know.
Also I'm on my BlackBerry so I will attempt to attach a copy of the comic, idk how well it work, if at all.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Translate a simple script from c to c++

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 12:51 AM

You nailed it as far as I'm concerned.

#include <iostream>
using namespace std;

int main() {

    for (int x = 0; x < 500; x++) {
        cout << "I will not throw paper airplanes in class";
    }

    return 0;
}


Fixed your error ('>>' is incorrect. It is cout <<). :)
Was This Post Helpful? 1
  • +
  • -

#3 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 12:53 AM

Or...

#include <iostream>
using namespace std;

int main() {

    do {
         cout << "I will not throw paper airplanes in class";
    } while (int x = 0; x < 500; x++);

    return 0;
}



... is how I would code it :)

Either way, same results.

This post has been edited by pr4y: 20 February 2009 - 12:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 <binary_glitch>  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 27-November 08

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 01:26 AM

Wow That was fast, it took two breaks + to type that and I'm taking an other just to read my replies, no worries about my job... I'm a grave-yard cook so I have nothing to do. Um well I cut my code from anjuta and pasted it to my 'usb mass storage device' (my BlackBerry) and re-pasted it into the forum so the << corection will probably work, thanx. Also great job funding different ways to code it, that helps me since I moved on from hello world and basic strings,(probably too earley) I didn't even know you could repeat a string... Never consitered it untill I saw this comic, now I'm learning all kinds of ways to do it! Can any function be repeated in a loop x amount of times, like that? Sorry I know, I'm a newbe, but I was in a hurry to get a means to an end when I started that, so I researched what I needed to, to repeat or loop as I am learning it's called a string.
Was This Post Helpful? 0
  • +
  • -

#5 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 10:15 AM

Yes! You're absolutely right again, and thanks for using the green button, it adds to my "Thanked: x times" counter. :P

#include <iostream>
using namespace std;

int main() {

     for (int x = 0; x < 50; x++) {
          printf("%i\n", x); /* C-style */
     }

     return 0;
}


or

#include <iostream>
using namespace std;

int main() {

     for (int x = 0; x < 50; x++) {
          cout << x << endl; // C++ style
     }

     return 0;
}


It will output the variable "x" every iteration ("run") of the loop. :)
You can also specify other variables of any type to have something done in a loop! Like this (I don't know if you know what a "string" is or not yet)...

#include <iostream>
using namespace std;

int main() {

     char Word[] = "Hello World!"; /* C-style string - an array of characters */

     for (int x = 0; x < 5; x++) {
          printf("%s\n", Word); /* C uses printf. %s is a token for "string" and \n is an "escape character" for a newline */
     }

     return 0;
}


The C++ equivalent would be as follows:
#include <iostream>
using namespace std;

int main() {

     string Word = "Hello World!"; // C++ string

     for (int x = 0; x < 5; x++) {
          cout << Word << endl; // "endl" means "end line" (it's a new line, it drops it down one)
     }

     return 0;
}


Welcome, hope this helps. :)
Happy coding, <binary_glitch>!
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 11:28 AM

Since it's C++, how about a class?
#include <iostream>
#include <string>

class PhraseRepeater
{
public:
   PhraseRepeater() : phrase_("Hello world"), repetitions_(10) {}
   PhraseRepeater(std::string phrase) : phrase_(phrase), repetitions_(10) {}
   PhraseRepeater(std::string phrase, unsigned int reps):
         phrase_(phrase), repetitions_(reps) {}

   void SpitItOut() const
   {
      for (unsigned int i = 0; i < repetitions_; ++i)
      {
         std::cout << phrase_ << std::endl;
      }
   }

   void SpitItOut(unsigned int reps) const
   {
      for (unsigned int i = 0; i < reps; ++i)
      {
         std::cout << phrase_ << std::endl;
      }
   }

   static void SpitItOut(std::string phrase, unsigned int reps)
   {
      for (unsigned int i = 0; i < reps; ++i)
      {
         std::cout << phrase << std::endl;
      }
   }

private:
   std::string phrase_;
   unsigned int repetitions_;
};

int main()
{
   PhraseRepeater pr1;
   pr1.SpitItOut();
   pr1.SpitItOut(20);

   PhraseRepeater pr2("Goodbye cruel world");
   pr2.SpitItOut();
   pr2.SpitItOut(20);

   PhraseRepeater pr3("Hello again", 5);
   pr3.SpitItOut();
   pr3.SpitItOut(15);

   PhraseRepeater::SpitItOut("I'm Outta Here", 5);

   return 0;
}

Was This Post Helpful? 0
  • +
  • -

#7 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 12:13 PM

JackOfAllTrades, You forgot to give the C equivalent with a structure. :(

You lose.
Was This Post Helpful? 0
  • +
  • -

#8 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 12:21 PM

View PostHyper, on 20 Feb, 2009 - 09:15 AM, said:

Yes! You're absolutely right again, and thanks for using the green button, it adds to my "Thanked: x times" counter. :P



Was my suggestion not as helpful? I feel left out :(

All is good in Mr. Roger's Neighborhood... I'm not too worried about my "Thanks", as long as I know I helped, thats all that matters :)
Was This Post Helpful? 1
  • +
  • -

#9 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 12:23 PM

pr4y, That happens to me all the time... Except I'm usually the first poster (as I was in this case). :P

I think your example was helpful, personally. But I'm not the OP, so I can't give "Thanks" to you :(
Was This Post Helpful? 1
  • +
  • -

#10 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 12:24 PM

You could just avoid all that run-time repetition altogether in C++; I don't think there's really a 'C' equivalent to this one
#include <string>
#include <iostream>

template<int N> struct spit_it_out : public spit_it_out<N-1>
{
    spit_it_out( std::string s ) : spit_it_out<N-1>(s) 
    { std::cout << s; }
};

template<> struct spit_it_out<0>
{
    spit_it_out( std::string s ) 
    { std::cout << s; }
};

int main()
{
    spit_it_out<10>("Hello, World\n");
} 

Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 01:07 PM

View PostHyper, on 20 Feb, 2009 - 02:13 PM, said:

JackOfAllTrades, You forgot to give the C equivalent with a structure. :(

You lose.


DOH! /facepalm

I like Bench's solution, too :)
Was This Post Helpful? 0
  • +
  • -

#12 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Translate a simple script from c to c++

Posted 20 February 2009 - 01:48 PM

Well <binary_glitch>, hopefully this was helpful to you! :)

pr4y, I'm aware you can give "thanks" even if it's not to the original poster, but I think that's wrong. It shouldn't come from me, but the poster who you were trying to help. :/
Was This Post Helpful? 0
  • +
  • -

#13 <binary_glitch>  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 27-November 08

Re: Translate a simple script from c to c++

Posted 21 February 2009 - 12:18 AM

If it where not such a major ordeal for me to navagate DIC on my BlackBerry (which happens to be my only connection to the internet) due to the slow loading of pages and the rendrence of pages, I would thank a lot more often. I did thank pr4y but I thanked you (hyper) for many reasons, the one that I was most thankfull for was that since I seen my compiler out put, and you foun the << vs. >> error I was prity sure you just fixed my code.
However I believe that you should thank someone only if they helped you. You should not thank someone for what you believe helped someone else, that's speaking for someone else. If you where doing a google search for something for example and your search brings you here and someone posts exactly the information you've been looking for, and that realy helped you, it would not be wrong for you to (join the forum if need be) and thank the poster of said usefull (to you) information and thank them, even if you have nothing else to do with the thread. That's all just imho though. At any rate, I'd like to send a big thank you out to all who have and or will post anything that's usefull/helpfull etc. to me! Thanx A lot!
Was This Post Helpful? 0
  • +
  • -

#14 h3X  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 30-January 09

Re: Translate a simple script from c to c++

Posted 21 February 2009 - 02:50 AM

Why are you using capital letters in your datatypes/standard functions? Looks kinda wrong, and I think the compiler would complain about it.
Was This Post Helpful? 0
  • +
  • -

#15 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Translate a simple script from c to c++

Posted 22 February 2009 - 11:29 PM

View Post<binary_glitch>, on 20 Feb, 2009 - 11:18 PM, said:

If it where not such a major ordeal for me to navagate DIC on my BlackBerry (which happens to be my only connection to the internet) due to the slow loading of pages and the rendrence of pages, I would thank a lot more often. I did thank pr4y but I thanked you (hyper) for many reasons, the one that I was most thankfull for was that since I seen my compiler out put, and you foun the << vs. >> error I was prity sure you just fixed my code.
However I believe that you should thank someone only if they helped you. You should not thank someone for what you believe helped someone else, that's speaking for someone else. If you where doing a google search for something for example and your search brings you here and someone posts exactly the information you've been looking for, and that realy helped you, it would not be wrong for you to (join the forum if need be) and thank the poster of said usefull (to you) information and thank them, even if you have nothing else to do with the thread. That's all just imho though. At any rate, I'd like to send a big thank you out to all who have and or will post anything that's usefull/helpfull etc. to me! Thanx A lot!


I've had those before; Random "A Guest Thanked You!" type thanks on really old posts I've made. :)
Thank you for using the button that was put there, also thanks for thaking pr4y.

Thanks go where they're deserved (I forget how that saying goes).
Anyways, have anymore problems or questions? Feel free to post. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1