Basic C Question

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

50 Replies - 4007 Views - Last Post: 15 August 2005 - 06:57 PM Rate Topic: -----

#1 eLliDKraM  Icon User is offline

  • Pepè Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Basic C Question

Posted 13 August 2005 - 05:24 PM

This will be one of many basic questions I have. I just started C so don't break my neck.

I want to make a program that is real basic, just so I can say I did something.

The program will calculate a GPA but I'm not sure what to do. I want it to take in letter grades of let's say what Jimbo got on his progress report. I want it to be able to have a major flexibility in numbers and I don't want to assign a gajillion variables for however many grades there are to input. So let's say there are 3 grades he wants to put in, A, C,B+, i want the program to know that A would mean 4.0, C would mean 2.0, and B+ to mean 3.33 then I want it to add them up and divide it by the total number of grades Jimbo had input in the first place.

Where i'm mostly stuck is how to let there be a million some amount of grades to input (without having to make a ton of floats) and how to assign a letter grade a number.

Any help will be much appreciated, thank you.

This post has been edited by eLliDKraM: 13 August 2005 - 05:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Basic C Question

#2 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Basic C Question

Posted 13 August 2005 - 05:41 PM

a good way would be to have something like 1 variable that keeps track of a total, and another to keep track of the amount of grades entered;

throw all this into a loop. a + will add .33 to the total, and - will take away .33
so it would look like this.

A,A,A,B,A-,B+
or 4 + 4 + 4 + 3 + 4 - 0.33 +3 + 0.33

total = 22
grades_entered = 6

22 / 6 = 3.67

i would read in two characters at time. the first to be the grade, and the second is + or -. if the second character is blank, no effect.

might look like this
char buffer[3];
float total = 0.00;
int grades_enetered = 0;

while (1) //Or something else like a bool to break loop
{
     cin >> buffer;
     switch (buffer[0])
     {
      case 'A':
      {
           total += 4;
           grades_entered++;
      }
      break;
      //and so on..
      }

      if (buffer[1] == '+') total += 0.33;
     else if (buffer[1] == '-') total -= 0.33;
}


This post has been edited by Dark_Nexus: 13 August 2005 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 eLliDKraM  Icon User is offline

  • Pepè Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: Basic C Question

Posted 13 August 2005 - 05:51 PM

OK

This post has been edited by eLliDKraM: 13 August 2005 - 05:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Basic C Question

Posted 13 August 2005 - 05:52 PM

refresh i added some pseudo code to give you a general idea

EDIT: oh, and did you mean you wanted the grades to be taken as one big string, or one at a time?

This post has been edited by Dark_Nexus: 13 August 2005 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 eLliDKraM  Icon User is offline

  • Pepè Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: Basic C Question

Posted 13 August 2005 - 05:57 PM

What is cin? Is that like scanf? I guess I would modify it later if I wanted it as a string or individually but according to your code, I should never have started C in the first place because I don't know what the hell i'm doing.
Was This Post Helpful? 0
  • +
  • -

#6 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Basic C Question

Posted 13 August 2005 - 06:04 PM

lol, you can replace my switch statement with ifs if you please

if (buffer[0] == 'A')
{
total += 4; //total = total + 4
grades_entered++ //grades_entered = grades_entered + 1
}
else if (buffer[0] == 'B')
{
total += 3; //total = total + 3;
grades_entered++;
}
//and so on...all the way to F

if (buffer[1] == '+) total += 0.33 //total = total + 0.33


and yes cin is similar to scanf as it gets input from the usser, because buffer is 3 characters long, it will only get 3 characters from the user, or 1-2 characters and a return "\n"

what exactly are you confused about?

This post has been edited by Dark_Nexus: 13 August 2005 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Basic C Question

Posted 13 August 2005 - 06:07 PM

errr, sorry didn't see the "C" part, the code i showed you was in c++

sorry :/

you can still treat it as pseudo code though to get the general idea :P
Was This Post Helpful? 0
  • +
  • -

#8 eLliDKraM  Icon User is offline

  • Pepè Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: Basic C Question

Posted 13 August 2005 - 06:11 PM

Quote

if (buffer[0] == 'A')
What is the 0 and why is it in brackets?
Was This Post Helpful? 0
  • +
  • -

#9 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Basic C Question

Posted 13 August 2005 - 06:16 PM

the []'s mean it's part of an array

i said char buffer[3]' <-array

buffer[0] means the first element of that array

buffer[2] means the third character
Was This Post Helpful? 0
  • +
  • -

#10 eLliDKraM  Icon User is offline

  • Pepè Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: Basic C Question

Posted 13 August 2005 - 06:24 PM

main() {

char buffer[3];
float total=0.0;
int grades_entered=0;

if (buffer[0]==A) {
total+=4;
grades_entered++;
}

else if (buffer[0]==B) {
total+=3;
grades_entered++;
}

else if (buffer[0]==C) {
total+=2;
grades_entered++;
}

else if (buffer[0]==D) {
total+=1;
grades_entered++;
}

else if (buffer[0]==F) {
total+=0;
grades_entered++;
}

if (buffer[1]==+) {
total+=0.33
grades_entered++;
}

else if (buffer[1]==-) {
total-=0.33
grades_entered++;
}


That's a lot of code for a bunch of nothing, but is it right so far? I'm also missing my printf's and scanf's but am not sure where to stick them.
Was This Post Helpful? 0
  • +
  • -

#11 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Basic C Question

Posted 13 August 2005 - 06:35 PM

looks ok so far,

you will want to either

A) implement those if statements into a loop so that you can get one grade at a time, and then analyze the grade inputed

-get input from user
-analyze input to equate to a grade
-return to top of loop

or B)have the user enter one big string of grades delimitted by commas for instance.

i would say A) is the easiest for now

EDIT: oh also, you have main() {

if you just say main(), it defaults to int main(), so at the bottom of the function you will want to return 0;

EDIT EDIT: in the if's you are saying

if (buffer[0] == A)

you actually have to put that A in single quotes

if (buffer[0] == 'A')

This post has been edited by Dark_Nexus: 13 August 2005 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#12 eLliDKraM  Icon User is offline

  • Pepè Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: Basic C Question

Posted 13 August 2005 - 06:47 PM

Would you be so kind as to give me an example to your option A?

I thank you much for your helping me.

This post has been edited by eLliDKraM: 13 August 2005 - 07:23 PM

Was This Post Helpful? 0
  • +
  • -

#13 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 16
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: Basic C Question

Post icon  Posted 13 August 2005 - 09:58 PM

I'm not sure what Dark Nexus had in mind, but if it were me....

Do you have a set number of grades that will be put in each time through the program, or will the number vary? You could set up a
for(i=0; i<=num; i++)
loop for a specific number of grades (for example: the number of classes you have per quarter, plus pvious data), or you could use a
while(true)
loop to run the program until you enter a sentinel.

Keep in mind, I'm still learning C++ myself, and Dark Nexus is undoubtedly is much better than I. Take my suggestions for what they're worth.... :P

This post has been edited by SPlutard: 13 August 2005 - 09:59 PM

Was This Post Helpful? 0
  • +
  • -

#14 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Basic C Question

Posted 13 August 2005 - 10:45 PM

lol, we pretty much said the same thing.

I still dunno how exactly he wants to get the grades in, with an entire string on what. however if you wanted to just get one grade at a time, i would agree with (also repeat myself) SPlutard.

while (1)
{
     //All that shite i showed you before
    if (buffer[0] == 'N') //or some other character to show the end of the input
          break;
}


Was This Post Helpful? 0
  • +
  • -

#15 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 16
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: Basic C Question

Post icon  Posted 13 August 2005 - 10:48 PM

:D I'm not sure he knows what he wants....
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »