We lost our programming virginity - Page 18 - Overclock.net - An Overclocking Community

Forum Jump: 

We lost our programming virginity

Reply
 
Thread Tools
post #171 of 180 (permalink) Old 11-02-2009, 02:34 PM
New to Overclock.net
 
Metruzero's Avatar
 
Join Date: Oct 2009
Location: In a box
Posts: 11
Rep: 1 (Unique: 1)
For a lot of people, losing your programming virginity is big moment, have the feeling that YOU made this. You made this program to find the average test score of 3 tests. And it only took you 20 minutes or so. That feeling can really get people thinking, "Wow! This is fun!" I can honestly say, even now that I'm much better at programming, I still jump up and go WHOO whenever I get something working. It's just a good feeling when you work hard on a program and then you see it work. So me, I can honestly say, that losing my programming virginity is one of the best things that ever happened to me. And it's either programming or watching TV and getting fat, and I hate fat.
Metruzero is offline  
Sponsored Links
Advertisement
 
post #172 of 180 (permalink) Old 11-09-2009, 12:28 AM - Thread Starter
New to Overclock.net
 
TheGrayNobleman's Avatar
 
Join Date: Jun 2007
Location: Cali
Posts: 1,243
Rep: 111 (Unique: 104)
Anyone got any suggestions for my code?

It is a grading app that takes information from a file and reads it.
Input file
Code:
3 
1000  9.5  9.0  8.5  87.0  92.5  86.0 
2000 10.0  10.0 10.0 100.0 100.0 100.0 
3000  8.0  8.0  8.0  80.0  80.0  79.0
App
Code:
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
// Two file stream objects for input and output.
ifstream in_file;
ofstream out_file;

//Student and test numbers
int students_num, id;
double exam1, exam2, exam3, midterm1, midterm2, final, average;
int highperson = 0;
double highscore = 0;


//Decimal Fixs
cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2); 

out_file.setf(ios::fixed);
out_file.setf(ios::showpoint);
out_file.precision(2);

//File input
char file_name[80];
cout<< "Enter an input file name: ";
cin>> file_name;
in_file.open(file_name);

//in_file.open("C:\\\\Temp\\\\score.txt");
out_file.open("C:\\\\Temp\\\\Student_Grades.txt");

//File errors
if (in_file.fail()) {
    cout << "Error: input file open failed.\
";
    exit (1);
}

if (out_file.fail()) {
    cout << "Error: output file open failed.\
";
    exit (1);
}

// Get the number of students.
in_file >> students_num;

cout << "--------------------------------------------------------" << endl
<< "Course Report" << endl
<< "--------------------------------------------------------" << endl;

out_file << "--------------------------------------------------------" << endl
<< "Course Report" << endl
<< "--------------------------------------------------------" << endl;

//Average and grade loop
for(int i=0; i < students_num; i++) {
        in_file >> id >> exam1 >> exam2 >> exam3 >> midterm1 >> midterm2 >> final;
    
//Calculate average
double exam, midterm;
exam = (exam1*10+exam2*10+exam3*10)/3;
midterm = (midterm1+midterm2)/2;
average = (exam*.2)+(midterm*.4)+(final*.4);

    // Display the id and average
    cout << id << ' ' << average;

    // Output the id and average to the output data file.
    out_file << id << ' ' << average;

//Letter grade
char grade;

if (average > 89){
grade = 'A';
}
else if (average > 79){
grade = 'B';
}
else if (average > 69){
grade = 'C';
}
else if (average > 59){
grade = 'D';
}
else {
grade = 'F';
}

//Output grade
cout << " " << grade << endl;
out_file << " " << grade << endl;

//Finds highest grade
if (average > highscore){
highscore = average;
highperson = id;
}
else{
cout << "";
}
} //End for()

cout << "--------------------------------------------------------" << endl;
out_file << "--------------------------------------------------------" << endl;

//High score display
cout << endl << "*** The highest scoring student is " << highperson << "! (Average = " << highscore << ") ***" << endl << endl;
out_file << endl << "*** The highest scoring student is " << highperson << "! (Average = " << highscore << ") ***" << endl << endl;

//Close files
in_file.close();
out_file.close();

//Break
return 0;

} //End main()

Quote:
Originally Posted by Syrillian go_quote.gif
The inherent and insurmountable shortcoming is that the entire framework of human understanding is steeped in finite terms. Everything has a beginning, everything has an end.... we simply can not fathom anything before or after.

Infinite is not necessarily a word, it is more of a sound that has a connotation or a theory, as by definition... we can not define it.

Infinite Time and infinite Space. Attempting to define them is not only ironic, it is an act of futility.
TheGrayNobleman is offline  
post #173 of 180 (permalink) Old 11-09-2009, 08:19 AM
New to Overclock.net
 
Join Date: Jun 2008
Location: Huntington Beach, CA
Posts: 1,195
Rep: 81 (Unique: 72)
i cant really help you with file operations, because i always used the stdio file operations, and never once tried fstream. Sorry


Click to see epic quotes! (Click to show)Warning: Spoiler! (Click to show)
Quote:
Originally Posted by Pir go_quote.gif
Don't you have an AMD pen lying around somewhere? They write a bit slower but at least they always do.
Quote:
Originally Posted by Hephasteus go_quote.gif
CPU's used to have to do more work, they were fighting off wolves with flyswatters, today they are swatting at flys with bazookas.
Quote:
Originally Posted by nathris go_quote.gif
OSX User: Oh look funny dog pictures, I'm going to download them!
"Funny Dog Pictures requires that you type your password"
OSX User: Hurr Durr, OK!.



Join the deaf club...
sailerboy is offline  
Sponsored Links
Advertisement
 
post #174 of 180 (permalink) Old 11-09-2009, 04:39 PM - Thread Starter
New to Overclock.net
 
TheGrayNobleman's Avatar
 
Join Date: Jun 2007
Location: Cali
Posts: 1,243
Rep: 111 (Unique: 104)
Quote:
Originally Posted by sailerboy View Post
i cant really help you with file operations, because i always used the stdio file operations, and never once tried fstream. Sorry
How is stdio different from fstream? (It never hurts to learn alternate methods, especially for someone like me with little experience )

Quote:
Originally Posted by Syrillian go_quote.gif
The inherent and insurmountable shortcoming is that the entire framework of human understanding is steeped in finite terms. Everything has a beginning, everything has an end.... we simply can not fathom anything before or after.

Infinite is not necessarily a word, it is more of a sound that has a connotation or a theory, as by definition... we can not define it.

Infinite Time and infinite Space. Attempting to define them is not only ironic, it is an act of futility.
TheGrayNobleman is offline  
post #175 of 180 (permalink) Old 11-09-2009, 04:58 PM
New to Overclock.net
 
Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,282
Rep: 643 (Unique: 391)
Quote:
Originally Posted by TheGrayNobleman View Post
How is stdio different from fstream? (It never hurts to learn alternate methods, especially for someone like me with little experience )
stdio contains the C i/o functions, iostream et al. contain the C++ i/o classes and such

rabidgnome229 is offline  
post #176 of 180 (permalink) Old 11-09-2009, 05:03 PM
New to Overclock.net
 
Join Date: Jun 2008
Location: Huntington Beach, CA
Posts: 1,195
Rep: 81 (Unique: 72)
for example, i would use fopen to open a file, fprintf to print to a file, fscanf to read a file, fseek to go to a specific part in the file, and ftell to find where i was. Much simpler to me.


Click to see epic quotes! (Click to show)Warning: Spoiler! (Click to show)
Quote:
Originally Posted by Pir go_quote.gif
Don't you have an AMD pen lying around somewhere? They write a bit slower but at least they always do.
Quote:
Originally Posted by Hephasteus go_quote.gif
CPU's used to have to do more work, they were fighting off wolves with flyswatters, today they are swatting at flys with bazookas.
Quote:
Originally Posted by nathris go_quote.gif
OSX User: Oh look funny dog pictures, I'm going to download them!
"Funny Dog Pictures requires that you type your password"
OSX User: Hurr Durr, OK!.



Join the deaf club...
sailerboy is offline  
post #177 of 180 (permalink) Old 11-10-2009, 02:39 PM
New to Overclock.net
 
Join Date: Sep 2007
Location: Seaside, CA
Posts: 6,125
Rep: 393 (Unique: 296)
Code:
#include <iostream>
using namespace std;

int add(double num1, double num2);
int sub(double num1, double num2);
int multiply(double num1, double num2);
int divide(double num1, double num2);

int main()
{
double num1, num2;
int selection;

cout << "What do you want to do?" << endl
<< "<1> Add" << endl
<< "<2> Subtract" << endl
<< "<3> Multiply" << endl
<< "<4> Divide" << endl
<< "Selection: ";
cin >> selection;

if (selection >= 1 && selection <=4)
{
cout << "First number: ";
cin >> num1;
cout << "Second number: ";
cin >> num2;
}

switch(selection)
{
case 1:
add(num1, num2);
break;
case 2:
sub(num1, num2);
break;
case 3:
multiply(num1, num2);
break;
case 4:
divide(num1, num2);
break;
default:
cout << "Learn to choose a correct number dip****" << endl;
return 0;
}

return 0;
}

int add(double num1, double num2)
{
double total;
total = num1 + num2;

cout << num1 << " + " << num2 << " is " << total << endl;

return 0;
}

int sub(double num1, double num2)
{
double total;
total = num1 - num2;

cout << num1 << " - " << num2 << " is " << total << endl;

return 0;
}

int multiply(double num1, double num2)
{
double total;
total = num1 * num2;

cout << num1 << " * " << num2 << " is " << total << endl;

return 0;
}

int divide(double num1, double num2)
{
double total;
total = num1/num2;

cout << num1 << " / " << num2 << " is " << total << endl;

return 0;
}
I was bored. Made a quick calculator. Anything glaringly wrong?

Only thing I think I may add is a dedicated output function. Gets rid of the repeated "num1 +/-/* num2 = bleh" stuff in the code.

EDIT: Fixed a little bug...

~Gooda~


Start folding HERE!
Quote:
Originally Posted by alexgheseger go_quote.gif
Wow...

I never thought i'd have to say this, but any penis pictures will net you a hefty infraction mmkay?
Engineering Sample CPUs: Q6600 B0 - Xeon E3110 (E8400) E0 - E5400 (possibly) - E8400 (possibly)
Havegooda is offline  
post #178 of 180 (permalink) Old 11-10-2009, 04:27 PM
New to Overclock.net
 
Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,282
Rep: 643 (Unique: 391)
Why do the add/subtract/etc methods all return 0?

rabidgnome229 is offline  
post #179 of 180 (permalink) Old 11-10-2009, 06:45 PM
New to Overclock.net
 
Join Date: Sep 2007
Location: Seaside, CA
Posts: 6,125
Rep: 393 (Unique: 296)
Quote:
Originally Posted by rabidgnome229 View Post
Why do the add/subtract/etc methods all return 0?
Good question...I changed them to void functions.

~Gooda~


Start folding HERE!
Quote:
Originally Posted by alexgheseger go_quote.gif
Wow...

I never thought i'd have to say this, but any penis pictures will net you a hefty infraction mmkay?
Engineering Sample CPUs: Q6600 B0 - Xeon E3110 (E8400) E0 - E5400 (possibly) - E8400 (possibly)
Havegooda is offline  
post #180 of 180 (permalink) Old 11-13-2009, 08:07 AM
New to Overclock.net
 
Join Date: Jun 2008
Location: Huntington Beach, CA
Posts: 1,195
Rep: 81 (Unique: 72)
change THIS
Code:
    cout << "What do you want to do?" << endl
        << "<1> Add" << endl
        << "<2> Subtract" << endl
        << "<3> Multiply" << endl
        << "<4> Divide" << endl
        << "Selection: ";
    cin >> selection;

    if (selection >= 1 && selection <=4)
    {
        cout << "First number: ";
        cin >> num1;
        cout << "Second number: ";
        cin >> num2;
    }
TO
Code:
    do{cout << "What do you want to do?" << endl
        << "<1> Add" << endl
        << "<2> Subtract" << endl
        << "<3> Multiply" << endl
        << "<4> Divide" << endl
        << "Selection: ";
cin >> selection;
}while(selection < 1 || selection > 4)
    cout << "First number: ";
    cin >> num1;
    cout << "Second number: ";
    cin >> num2;

    }
A do/while loop runs through the loop at least once before checking the boolean statement. It is useful sometimes.


Click to see epic quotes! (Click to show)Warning: Spoiler! (Click to show)
Quote:
Originally Posted by Pir go_quote.gif
Don't you have an AMD pen lying around somewhere? They write a bit slower but at least they always do.
Quote:
Originally Posted by Hephasteus go_quote.gif
CPU's used to have to do more work, they were fighting off wolves with flyswatters, today they are swatting at flys with bazookas.
Quote:
Originally Posted by nathris go_quote.gif
OSX User: Oh look funny dog pictures, I'm going to download them!
"Funny Dog Pictures requires that you type your password"
OSX User: Hurr Durr, OK!.



Join the deaf club...
sailerboy is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off