First week of programming

After almost five years I now return to programing. Prior to beginning this course I have had some experience in using Java but not at any advanced level. Having written code before helped me to understand all the lectures so far, but I have a feeling that with the speed we move on to new things I’ll be lost next week.

Being the first week I don’t really have any interesting code to talk about. The most fun exercise was to create the “bottles of X on the wall” program. I found a website (http://www.99-bottles-of-beer.net/) where user could submit a solution in different languages where some tried to create the most advanced version and some the most compact version. My code is more primitive than those:

#include <iostream>

int main(int argc, char*arhv[]){

int bottles;

std::cout << ”Decide the number of bottles!”;
std::cin >> bottles;
std::cout << bottles << bottles of juice on the wall, one fell down” << std::endl;

while (bottles > 0){
bottles–;
std::cout << bottles << ” bottles of juice on the wall, one fell down” << std::endl;
}
if (bottles == 0){
std::cout << ”No bottles left on the wall!” << std::endl;

}

std::cin.ignore(1024, ‘\n’);
std::cin.get();
return 0;

}

If I look on this code later I could probably find a better way to do this. In my first version I used int userInput; with bottles = userInput resulting in some very unnecessary extra code.

Lämna en kommentar