help with programming

Legend_of_joe

Cheng's Errand Boy
Joined
Sep 1, 2005
Posts
120
hi are there any good c++ programmers here? im taking a class in it and its really hard. any good tips for me? my first assignment is almost due and im busting my brains out trying to get it done. i could really use some help. i have a bad habit of use goto instead of loops. please help me.
 

Neco_Coneco

Enemy Chaser
Joined
Oct 14, 2004
Posts
1,161
Tell us what the program needs to do. There is no magic advice I can give to help but with a description i can set you in the right direction.
 
Last edited:

ki_atsushi

So Many Posts
No Time
For Games.
20 Year Member
Joined
Mar 27, 2005
Posts
23,647
Legend_of_joe said:
hi are there any good c++ programmers here? im taking a class in it and its really hard. any good tips for me? my first assignment is almost due and im busting my brains out trying to get it done. i could really use some help. i have a bad habit of use goto instead of loops. please help me.

Good luck buddy, I got tired of C++ after 2 semesters of it. I hated programming in that language so much that I changed my major from computer science.
 

Neco_Coneco

Enemy Chaser
Joined
Oct 14, 2004
Posts
1,161
ki_atsushi said:
Good luck buddy, I got tired of C++ after 2 semesters of it. I hated programming in that language so much that I changed my major from computer science.


Once you get the basics down it gets much better. I am going into my senior year next Wednesday. It seems CS does have one of the highest drops rates in my college atleast.

I like working in C rather than java but I must admit i had the best time learning jsps and servlets
 
Last edited:

Legend_of_joe

Cheng's Errand Boy
Joined
Sep 1, 2005
Posts
120
the program generates two random numbers 0 1 and 2. but if they both equal 0 then i need to generate them again until they dont. up until now i've used if (x==0 && y==0) {goto start;} and placed start at the begining of the random number generator. my teacher doesnt like students to use goto and i dont know how to elimate it while keeping the program functionality. please help its due tomarrow :crying:
 

Neco_Coneco

Enemy Chaser
Joined
Oct 14, 2004
Posts
1,161
Legend_of_joe said:
the program generates two random numbers 0 1 and 2. but if they both equal 0 then i need to generate them again until they dont. up until now i've used if (x==0 && y==0) {goto start;} and placed start at the begining of the random number generator. my teacher doesnt like students to use goto and i dont know how to elimate it while keeping the program functionality. please help its due tomarrow :crying:

Since you seem to already have the methods down here is just a simple way to avoid the go to
(psuedo code)

randomMethod();

while(x==0 && y==0)
{
randomMethod();
}

print;
 
Last edited:

kobylka68

Basara's Blade Keeper
Joined
Jul 12, 2003
Posts
3,666
ki_atsushi said:
Good luck buddy, I got tired of C++ after 2 semesters of it. I hated programming in that language so much that I changed my major from computer science.

Heh, I skipped the whole class in my second year of computer science. Now I have to take it this semester if I plan on graduating on time.
 

Neco_Coneco

Enemy Chaser
Joined
Oct 14, 2004
Posts
1,161
kobylka68 said:
Heh, I skipped the whole class in my second year of computer science. Now I have to take it this semester if I plan on graduating on time.

I am in no rush to jump into the working world now. It seems the market for cs majors has all but dried up. It looks like I will need a masters for sure now :crying:


Also some advice to the OP never take solutions you found online. You would be surprised how many people in your class used the same one :kekeke: I dont know how many people i have seen get 0's or even fail a class for ripping some code online. Its better to fuck up an assignment get low grade but still pass. Plus in first year classes they never grade that hard. People use to have a whole chunk that did nothing and still get a 70
 
Last edited:

barf

Maxima's Barber
Joined
Aug 10, 2004
Posts
2,366
dunno your level... i m a bit rusty but guess that could help

Code:
int sub_gen()
{
// rand() returns a random integer between 0 and a big number
// so rand()%3 will return the modulo 3 of that number and hence
// 0,1 or 2 randomly (with a distribution close to gaussian in VC)
   return rand()%3;
}

// takes in parameter 2 int pointers
void random_generator(int* x,int* y)
{
// repeat the bitwise-or operation between 2 generated numbers
// while both equal zero
    while(!(*x=sub_gen()|*y=sub_gen()));
    return;
}

int main(int argv, char* argc or something like that)
{
     int x,y;
// initialize the random generator (seeding)
     srand();
// generate the random numbers (passing x and y by memory adress as parameter)
     random_generator(&x,&y);
// display
     printf("My two numbers are: %d and %d\n", x,y);
     return 0;   
}
i m a bit drunk, sorry for typos

edit: hope you ve learned pointers and bitwise operators... otherwise ask what you don t understand

edit: few comments
 
Last edited:

Legend_of_joe

Cheng's Errand Boy
Joined
Sep 1, 2005
Posts
120
Thanks for the help guys but i think im just going to turn it in as is goto's and all. I would have to totally restructure the program to put those while loops in there. I'm sure he won't doc me too many points since my program works perfectly despite the structure. next time though i'll use loops instead.

Legend_of_joe
 
Last edited:

Big Shady

Kyukyogenryu Black Belt
15 Year Member
Joined
Apr 16, 2003
Posts
4,945
The goto's are very inefficent, making your program longer then it has be. This project can be done in less then 10 lines with a while loop.
 
Last edited:

barf

Maxima's Barber
Joined
Aug 10, 2004
Posts
2,366
Big Shady said:
The goto's are very inefficent, making your longer then it has be. This project can be done in less then 10 lines with a while loop.

yep... goto in C = teh meh...

without using functions it fits in 7 lines (with the include :) )

Code:
#include <stdio> // or whatever includes printf (iirc rand doesn t need an include)
int main(int argv, char* argc or something like that) {  
     int x,y;
     srand();
     while(!((x=rand()%3)|(y=rand()%3)));
     printf("My two numbers are: %d and %d\n", x,y);
     return 0; }
 

Heinz

Parteizeit
15 Year Member
Joined
Feb 13, 2005
Posts
22,465
well im pretty much a noob in c++ but i only started a week ago (learning off of tutorials from the net)
Here is one of my apps but for some reason the "else if doesnt work... only the if


#include <iostream>

using namespace std;

int main()
{
int name;
int lachlan = 1;
int tim = 2;

cout<<"please enter your name: \n";
cin>> name;
cin.ignore();
if ( name = 1 ) {
cout<<"your cool\n";
}
else if ( name = 2 ) {
cout<<"wrong name buddy\n";
}
system("PAUSE");
return 0;
}
 

barf

Maxima's Barber
Joined
Aug 10, 2004
Posts
2,366
common starter mistake

if ( name = 1 ) {

will allways be true...

you must use name == 1 for comparision

name = 1 is seting name to the value 1.
 

ki_atsushi

So Many Posts
No Time
For Games.
20 Year Member
Joined
Mar 27, 2005
Posts
23,647
kobylka68 said:
Heh, I skipped the whole class in my second year of computer science. Now I have to take it this semester if I plan on graduating on time.

Wow, good luck man. Personally I rather deal with computer hardware than software. Programming is too tedious for me. I don't have the patience.
 
Top