Top.Mail.Ru
The C++ Community's Journal -- Day
? ?
The C++ Community's Journal -- Day [entries|friends|calendar]
The C++ Community

[ userinfo | livejournal userinfo ]
[ calendar | livejournal calendar ]

I did it guys!! [07 May 2007|06:13pm]

I finally wrote a complex (at least for my level) C++ program for class! We had to write a program that asks for a student's name, ask how many credits and the grade, and the output the student's name, Total credits, and GPA (GPA being the total of all the (credits times grade) for the classes divided by the total number of classes. This is what I got:


#include <iostream.h>
#include <stdlib.h>

int multiply (int x, int y)
{ int z;

  z = x*y;
  return z;
  }

float divide (float p, float q)
{float r;

   r = p/q;
   return r;
   }

int main()
{ char student [20], grade, ans, Y;
  int sum, ob, numcredits, numclasses, Totalcredits, A, B, C, D, someNum, coursegrade, coursegrades;
  float Mult, Div, avg;

sum= 0;
coursegrades= 0;

 cout << "Would you like to find out your GPA?: ";
 cin >> ans;

 while (ans=='Y')
      {

   cout << "What is your name?: ";
   cin >> student;

    cout << "How many classes?: ";
    cin >> numclasses;
   
    for (int i = 0; i < numclasses; i++)
     {
 cout << "How many credits for class?:  " << "\n";
 cin >> numcredits;
   
    sum = sum + numcredits;
   
 cout << "What is grade in class?: " << "\n";
 cin >> grade;
 if (grade=='A')
     {someNum=4;
     coursegrade = multiply (someNum, numcredits);
     }      
       else if (grade=='B')
       {someNum=3;
        coursegrade = multiply (someNum, numcredits);
        }
       else if (grade=='C')
       {someNum=2;
       coursegrade = multiply (someNum, numcredits);
       }
       else if (grade=='D')
       {someNum=1;
       coursegrade = multiply (someNum, numcredits);
       }
       else if (grade=='F')
       {someNum=0;
       coursegrade = multiply (someNum, numcredits);
       } 
      coursegrades = coursegrades + coursegrade;  
      
       }           
          
   ob = coursegrades;    
 
    Totalcredits =  sum;
   
    avg = divide (ob, Totalcredits);       
    
cout << "Name: " << student << "\n";
cout << "Total Credits: " << Totalcredits << "\n";
cout << "GPA: " << avg << "\n";

cout << "\n";

cout << "Would you like to find out your GPA?: ";
cin >> ans;

      system("PAUSE");
      return 0;
}

You can check it out if you want, but it seems to be working fine for me according to what the teacher asked the program to do. I'm surprised I was actually able to write this with little to no help (I asked for help on assigning a numerical value to a letter, but I figured it out before I got the response). Thanks for the inspiration guys!

21 comments|post comment

weird c++ problem [07 May 2007|08:01pm]
here's a function that i wrote for a program for school:

void calcloFN(int integer[], int & lo, int count)
{
          lo = integer[0];
	cout << "lo = " << lo << endl;
	int x = 1;

	cout << count << endl;
	
	while(x <= count)
	{
		if(integer[x] < lo)
			lo = integer[x];
		cout << "lo = " << lo << endl;
		x++;
	}
}


For some reason, lo is the right value until the last iteration of the while loop, which then goes to 0. WTF is up?
9 comments|post comment

navigation
[ viewing | May 7th, 2007 ]
[ go | previous day|next day ]
Image