hulika

Author Topic: Anybody know C++?  (Read 2079 times)

Offline geetar_geek79

  • Forum Fanatic
  • ****
Anybody know C++?
« on: May 14, 2006, 11:14:20 AM »
I need help with some of my C++ school projects (6 left in all).  I'm totally lost with all of them.  I was able to do the first few of them but the later ones are pretty hard for me to grasp.

Here's the link of the assignments:
http://www.glendale.edu/knighton/C_C++_Programming/Assignments_C.html

Anybody?  Please help!...



Offline renz_sui

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #1 on: May 14, 2006, 04:14:03 PM »
Are you left with the last 6 ones or you're only required to answer the first 6 ones?

Offline geetar_geek79

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #2 on: May 14, 2006, 11:42:58 PM »
I'm left with the last six....    thanks!

sample codes, plz!

Offline bugoy

  • Philmusicus Addictus
  • *****
Anybody know C++?
« Reply #3 on: May 15, 2006, 12:18:19 AM »
matutulungan sana kta eh kaso tagal na ko d nag C++.. pag may extra time review ko and try ko gawin yan hehehe puro PHP na kc ako ngayon at ASP e

Offline geetar_geek79

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #4 on: May 15, 2006, 01:48:48 AM »
Quote from: bugoy
matutulungan sana kta eh kaso tagal na ko d nag C++.. pag may extra time review ko and try ko gawin yan hehehe puro PHP na kc ako ngayon at ASP e


slamat bosing!  ang hirap talaga eh, lalo na yung Classes at Structures.  Stuck din ako sa Arrays saka passing arguments.  Modular kasi lahat...


Offline renz_sui

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #5 on: May 15, 2006, 07:06:01 PM »
Ang gulo nung 5. Scanning characters from keyboard tas hindi dapat scanf or any other STDIO functions? pano yun? Assembly call?

Offline geetar_geek79

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #6 on: May 15, 2006, 11:24:46 PM »
nde...  gagawa ka ng sariling function na mayroong array.  doon mo ii-store yung bawat letter na input sa keyboard tapos terminate mo ng null string.  ayaw kasi ng proff namin gamitin namin yung built-in function ng C++.  Gusto raw nya kami matuto gumawa ng sariling functions saka mag-manipulate ng Arrays...  hirap nga, eh.  Napaka-tnga ko pa naman sa Arrays...

Slamat boss.

Offline sonnyrayvaughn

  • Philmusicus Addictus
  • *****
Anybody know C++?
« Reply #7 on: May 16, 2006, 12:06:20 AM »
hehe demm i shouldve taken C++... kung VB/VBA lang sana yan id be glad to help...

good luck bro!

Offline renz_sui

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #8 on: May 16, 2006, 08:30:26 AM »
Quote from: geetar_geek79
nde...  gagawa ka ng sariling function na mayroong array.  doon mo ii-store yung bawat letter na input sa keyboard tapos terminate mo ng null string.  ayaw kasi ng proff namin gamitin namin yung built-in function ng C++.  Gusto raw nya kami matuto gumawa ng sariling functions saka mag-manipulate ng Arrays...  hirap nga, eh.  Napaka-tnga ko pa naman sa Arrays...

Slamat boss.


ahh simple lang yan. Try mo tong code na to.

short get_str(char[] strPrompt, char[] strFilename, long MAX)
{
short i,n;
char[] strArray;

printf("%s",strPrompt);//print prompt
scanf("%s",&strArray);//okaya gets(strArray);

for(n=0;strArray[n]!='\0';n++);//count string input length

if(strlen(strArray)<MAX)//check if input exceeds maximum no. of strings
{
    for(i=0;i<n;i++)
        strFilename=strArray;//copies the contents char per char
 
    strFilename='\0';//null pointer for string.
    return i;//return the length of string
}

else
     return -1;//return something to denote exceeding of value

}

Offline namida

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #9 on: May 16, 2006, 11:36:53 AM »
ahh simple lang yan. Try mo tong code na to.

short get_str(char[] strPrompt, char[] strFilename, long MAX)
{
short i,n;
char[] strArray;

printf("%s",strPrompt);//print prompt
scanf("%s",&strArray);//okaya gets(strArray);

^^look out for this one. you have not allocated any space for strArray, this will lead to a very confusing debugging struggle, use fgets :D. Never use gets :D

WOA, do not use str*, that is reserverd for system use and will confuse the compiler :) kindly review standard naming conventions and rules :)


for(n=0;strArray[n]!='\0';n++);//count string input length

if(strlen(strArray)<MAX)//check if input exceeds maximum no. of strings
{
    for(i=0;i<n;i++)
        strFilename=strArray;//copies the contents char per char <-- strFilename must be big enough to hold strArray :)
 
    strFilename='\0';//null pointer for string. <-- you might overwrite the last character you copied on to the string :) i++ :D
    return i;//return the length of string
}

else
     return -1;//return something to denote exceeding of value
"Ano man ang iyong ginagawa sa iyong kapatid, ay siya ring ginagawa mo sa akin" --Yano

Home Recordings ->> http://www.soundclick.com/bands/default.cfm?bandID=480230  My Band ->>

Offline geetar_geek79

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #10 on: May 16, 2006, 01:51:18 PM »
Salamat po boss, I'll try the code...

Offline namida

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #11 on: May 16, 2006, 02:23:18 PM »
btw, what compiler and environment are you using?
"Ano man ang iyong ginagawa sa iyong kapatid, ay siya ring ginagawa mo sa akin" --Yano

Home Recordings ->> http://www.soundclick.com/bands/default.cfm?bandID=480230  My Band ->>

Offline renz_sui

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #12 on: May 16, 2006, 05:51:04 PM »
ahh simple lang yan. Try mo tong code na to.

short get_str(char[] strPrompt, char[] strFilename, long MAX)
{
short i,n;
char[] strArray;

printf("%s",strPrompt);//print prompt
scanf("%s",&strArray);//okaya gets(strArray);

^^look out for this one. you have not allocated any space for strArray, this will lead to a very confusing debugging struggle, use fgets :D. Never use gets :D

IIRC, fgets is for file get string; not something that you would take from the console keyboard input stream (in a standard sense) and IIRC C++ automatically allocates memory for Arrays (And I don't see why you need unallocated arrays). Yung sinasabi mo, pointers.

WOA, do not use str*, that is reserverd for system use and will confuse the compiler :) kindly review standard naming conventions and rules :)


Dude, we(programmers) all know that naming convention does not affect anything about the system AND is only used to help ease the reading of the program. str btw is reserved for structured datatypes in C not for system use. You do the reviewing.

for(n=0;strArray[n]!='\0';n++);//count string input length

if(n<MAX)//check if input exceeds maximum no. of strings
{
    for(i=0;i<n;i++)
        strFilename=strArray;
//copies the contents char per char <-- strFilename must be big enough to hold strArray :)
There's a predefined MAX, look for the bolded part

 
    strFilename='\0';//null pointer for string. <-- you might overwrite the last character you copied on to the string :) i++ :D
not sure about this part. Just put an i++ before putting the null at the end of the string if it overwrites the last char
    return i;//return the length of string
}

else
     return -1;//return something to denote exceeding of value
}[/b]

Offline geetar_geek79

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #13 on: May 17, 2006, 12:25:57 AM »
^SALAMAT!!!  I'll try the codes...

Offline namida

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #14 on: May 17, 2006, 03:09:47 AM »
Quote from: renz_sui

WOA, do not use str*, that is reserverd for system use and will confuse the compiler :) kindly review standard naming conventions and rules :)


Dude, we(programmers) all know that naming convention does not affect anything about the system AND is only used to help ease the reading of the program. str btw is reserved for structured datatypes in C not for system use. You do the reviewing.


Oops my bad, reserved identifiers rules only affect function names and global variables and not local variables :)
"Ano man ang iyong ginagawa sa iyong kapatid, ay siya ring ginagawa mo sa akin" --Yano

Home Recordings ->> http://www.soundclick.com/bands/default.cfm?bandID=480230  My Band ->>

Offline geetar_geek79

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #15 on: May 17, 2006, 07:06:43 AM »
Quote from: namida
btw, what compiler and environment are you using?


Dev C++
http://www.bloodshed.net/devcpp.html

Thanks...

Offline renz_sui

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #16 on: May 17, 2006, 10:31:56 AM »
Here's for the 6. Not really sure bout this one.

void assignment_6()
{
FILE *pFile;
int i,nLength;
char[] sFilename,sMode,sInput;
char cData;

get_str("Enter File Name:",sFilename,MAX);//Gets filename
get_str("Enter Mode (R/W)",sMode,MAX);//Gets mode

if(strcmp(sMode,"R")==0||strcmp(sMode,"r")==0)//if sMode gets R or r
{
if((pFile=fopen(sFilename, "rt"))!=NULL)//opens file for Reading and checks if it exists
{

do{
cData=fgetc(pFile);//gets the character from the file
printf("%c",cData);//prints cData
}while(cData!=EOF);//while it's not yet the end of the file
fclose(pFile);

/*the reason why I chose fgetc over fgets is because of the bug that fgets has with the EOF. I'm not sure if they already fixed the bug though*/

}
else //if it doesn't exists or something is wrong witht he file
puts("Error Opening File");
}

else if(strcmp(sMode,"W")==0||strcmp(sMode,"w")==0)//if sMode gets W or w
{
if((pFile=fopen(sFilename, "wt"))!=NULL)//Opens the filename for writing
{

do{
nLength=get_str("",sInput,MAX);//gets input from keyboard
for(i=0;i<nLength;i++)//loops the whole string char per char
fputc(pFile,sInput);//enters the whole string char per char except the '\0'
fputc(pFile,'\n');//inserts a line break
}while(strcmp(sInput,EOF)!=0);//loop to detect if the input from keyboard is an EOF

fclose(pFile);//closes the file

}

else//if file is in use
puts("Error Opening File");

}

else//if keyboard input is wrong
puts("Wrong input. Must be R or W only");

}

Offline geetar_geek79

  • Forum Fanatic
  • ****
Anybody know C++?
« Reply #17 on: May 17, 2006, 01:49:46 PM »
Wow...  ang lulupet tlga ng mg noypits sa programming (except me!)
SALAMAT!!!