Overclock.net - An Overclocking Community - Reply to Topic
Thread: We lost our programming virginity Reply to Thread
Title:
Message:

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


  Additional Options
Miscellaneous Options

  Topic Review (Newest First)
11-13-2009 08:07 AM
sailerboy 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.
11-10-2009 06:45 PM
Havegooda
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~
11-10-2009 04:27 PM
rabidgnome229 Why do the add/subtract/etc methods all return 0?
11-10-2009 02:39 PM
Havegooda
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~
11-09-2009 05:03 PM
sailerboy 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.
11-09-2009 04:58 PM
rabidgnome229
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
11-09-2009 04:39 PM
TheGrayNobleman
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 )
11-09-2009 08:19 AM
sailerboy i cant really help you with file operations, because i always used the stdio file operations, and never once tried fstream. Sorry
11-09-2009 12:28 AM
TheGrayNobleman 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()
11-02-2009 02:34 PM
Metruzero 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.
This thread has more than 10 replies. Click here to review the whole thread.

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