Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,540 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,521 people online right now. Registration is fast and FREE... Join Now!




c++ help for a beginner

2 Pages V  1 2 >  
Reply to this topicStart new topic

c++ help for a beginner

tikotiko
29 Oct, 2007 - 04:44 PM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
Assume that a class of 60 students have taken an exam and each student is likely to score any integer grade between 51 to 100. Simulate the grades of the students using the rand( ) function. A student will receive the following letter grades,

89< A <= 100, 79 < B < 90, 69 < C < 80, 59 < D < 70, 49 < F < 60

The program should output the total number of letter grades A, B, C, D, and F received by the class.


and this my code :
CODE

#include <cstdlib>
#include <iostream>
#include<string>
using namespace std;

int counter=0;

int studentGrade;
int acounter =0;
int bcounter = 0;
int ccounter = 0;
int dcounter = 0;
int fcounter = 0;



int main ()
{
        
  cout<<"\n please enter a grade ";
  cin >>studentGrade;
  
for ( int i=51;i<=99; i ++ )
{
int random_integer = (rand() % 50) + 51;


  
    if ( studentGrade >89 && studentGrade <= 100 )
   {
   cout << "A";
counter =  acounter;
acounter++;
}
else
   if (studentGrade >79 && studentGrade < 90 )
     {
      cout << "B";
  counter = bcounter;
bcounter++;
}
   else
      if (studentGrade >69 && studentGrade<80)
       {
         cout << "C";  
      counter=ccounter;
      ccounter++;
      }
      else
         if ( studentGrade >59 && studentGrade<70 )
            {
            cout << "D";
            counter=dcounter;
            dcounter++;
            }
            else
            if ( studentGrade >49 && studentGrade<60 )
            cout << " F";
            counter=fcounter;
   fcounter++;

cin>>studentGrade;

cin>>counter;

}
return 0;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: C++ Help For A Beginner
29 Oct, 2007 - 05:09 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Can you elaborate as the problem you are encountering? any error messages?
User is offlineProfile CardPM
+Quote Post

Binary_Ninja
RE: C++ Help For A Beginner
29 Oct, 2007 - 05:51 PM
Post #3

New D.I.C Head
*

Joined: 24 Oct, 2007
Posts: 48


My Contributions
I have a question, shouldnt that random() method be seeded with the time or something?
User is offlineProfile CardPM
+Quote Post

aceofspades686
RE: C++ Help For A Beginner
29 Oct, 2007 - 06:27 PM
Post #4

D.I.C Regular
Group Icon

Joined: 8 Oct, 2007
Posts: 261


Dream Kudos: 100
My Contributions
QUOTE(Binary_Ninja @ 29 Oct, 2007 - 09:51 PM) *

I have a question, shouldnt that random() method be seeded with the time or something?


If you want it to be slightly more "random" yes. You can use it like that, its just really predictable.
User is offlineProfile CardPM
+Quote Post

tikotiko
RE: C++ Help For A Beginner
29 Oct, 2007 - 06:50 PM
Post #5

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
QUOTE(aceofspades686 @ 29 Oct, 2007 - 07:27 PM) *

QUOTE(Binary_Ninja @ 29 Oct, 2007 - 09:51 PM) *

I have a question, shouldnt that random() method be seeded with the time or something?


If you want it to be slightly more "random" yes. You can use it like that, its just really predictable.



I just wanted to know if my code is correct or not , something else is that I don't get the counter as output..any help would be much appreciated
User is offlineProfile CardPM
+Quote Post

adeoluwafemi
RE: C++ Help For A Beginner
29 Oct, 2007 - 07:45 PM
Post #6

New D.I.C Head
*

Joined: 11 Oct, 2007
Posts: 5


My Contributions
1 thing tho. I dont see him declaring A B etc. Or is int acounter =0; also declaring A?
User is offlineProfile CardPM
+Quote Post

garima
RE: C++ Help For A Beginner
29 Oct, 2007 - 08:03 PM
Post #7

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 46


My Contributions
this thing can be done using a simple switch statement with a do while loop....just try it out
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: C++ Help For A Beginner
29 Oct, 2007 - 08:15 PM
Post #8

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
I think that you may be misinterpreting what you are supposed to do with this program.

If I read it correctly, you are supposed to be simulating grades for 60 students, using the rand() function to come up with the grades. There is NOTHING to suggest that you are supposed to be getting user input at any point. This would make all of your user input statements using cin unnecessary.

If you're trying to simulate a random grade, you need to either use the variable random_integer as the grade for each round through the loop (and get rid of your variable studentGrade), or assign the value of studentGrade using rand() in place of random_integer.

The control statement of your for loop also seems to be a little off. Since you are trying to cycle through 60 students, the condition in the for statement should be for(int i=0; i<60; ++i) - the current values in the loop will give you only 49 grades. Judging from the values that you've currently chosen for the for loop, it appears that you might have been trying to validate the user input - however, as I stated, it doesn't sound like you're actually supposed to be getting user input in this assignment. And even if you were, this would not be how to validate the user's input to ensure that the entered value is between 51 and 100.

And your use of the counter variable doesn't really make much sense here. You seem to be trying to increment the acounter, bcounter,...fcounter to be able to keep track of the number of the grades A-F. However, you're just assigning the value of whichever of these letter grade counters was last incremented to the counter variable - there doesn't seem to be much point to do so.

The following is a slightly altered and much pared-down version of your original code. I think it may be closer to what you are supposed to be doing, and will hopefully get you started:
CODE
#include <iostream>

using namespace std;

int acounter = 0;

int main() {
    for (int i=0; i<60; i++) {  //loop runs through 60 rounds
        int random_integer = (rand() % 50) + 51;//assigns a random number 51-100

        if (random_integer >89 && random_integer <= 100) {//test if random grade is in the range for an 'A'
            cout << random_integer << " ";
            cout << "A" << endl;
            acounter++;//increment acounter if grade is an 'A'
        }
    }
    cout << acounter << endl;
    return 0;
}


Hope that helps,

-jjh
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: C++ Help For A Beginner
29 Oct, 2007 - 08:32 PM
Post #9

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Looking at this again, I think that perhaps there have been more questions than answers for much of this thread. Hopefully I can clear a few of these up:

QUOTE

this thing can be done using a simple switch statement with a do while loop....just try it out

Switch statements are generally a good choice if you are dealing with different blocks of code for different integral values of a control variable. However, if you need to execute statements based on a range of potential values, it is frequently more efficient (at least in terms of the number of lines of code you have to write) to use if statements with relational expressions, combined with boolean operators - as the OP has done here.

QUOTE

1 thing tho. I dont see him declaring A B etc. Or is int acounter =0; also declaring A?

the declaration of acounter does not also declare a variable A. In this case, the "A", "B", etc values shown in the output stages are string literals. Because they are literals, rather than variables, they don't need to be declared. You can tell if something is a literal because it will be either a straight numerical value, for numerical literals (e.g. cout << 2;), it will be enclosed in single quotes, for literal characters (e.g. cout << 'A';), or it will be enclosed in double quotes, for string literals (e.g. cout << "string";).

QUOTE

QUOTE

I have a question, shouldnt that random() method be seeded with the time or something?

If you want it to be slightly more "random" yes. You can use it like that, its just really predictable.

aceofspades686 is right on this one - the builtin rand() function is actually pretty terrible as far as pseudo-random number generators go, and seeding it with the current system time will only slightly improve things. However, that really isn't critical unless you're dealing with encryption and stuff like that - it should be fine in cases like this where you really just need a sequence of numbers taken from a range, and you aren't worried about statistical correlation between sequentially generated numbers, distribution in space, etc. If you want a decent generator, check out the C++ snippets section- there are a couple of implementations of "good" RNGs there.


I hope that helps to clarify some of the issues that this thread has brought up.

-jjh
User is offlineProfile CardPM
+Quote Post

tikotiko
RE: C++ Help For A Beginner
29 Oct, 2007 - 09:19 PM
Post #10

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
Thank you very much jjhaag I really appreciate your help I have tried this code but it only gives me the number of grade A , I don't know where is the problem..

CODE

#include <iostream>
#include <cstdlib>
#include<string>

using namespace std;

int acounter = 0;
int bcounter = 0;
int ccounter = 0;
int dcounter = 0;
int fcounter = 0;
int main() {
    for (int i=0; i<60; i++) {  //loop runs through 60 rounds
        int random_integer = (rand() % 50) + 51;//assigns a random number 51-100

        if (random_integer >89 && random_integer <= 100) {//test if random grade is in the range for an 'A'
            cout << random_integer << " ";
            cout << "A" << endl;
            acounter++;
            }
        else
        if (random_integer >79 && random_integer < 90) {//test if random grade is in the range for an 'B'
            cout << random_integer << " ";
            cout << "B" << endl;
            bcounter++;
            }
            
            else
            
            if (random_integer >69 && random_integer < 80) {//test if random grade is in the range for an 'C'
            cout << random_integer << " ";
            cout << "C" << endl;
            ccounter++;
            
            }
            else
            if (random_integer >59 && random_integer < 70) {//test if random grade is in the range for an 'D'
            cout << random_integer << " ";
            cout << "D" << endl;
            dcounter++;
            }
            else
            if (random_integer >49 && random_integer <60) {//test if random grade is in the range for an 'F'
            cout << random_integer << " ";
            cout << "F" << endl;
            fcounter++;
        
        }
            
      
    }
    
    cout << " number of grade A is :" << acounter << endl;
   cin>>acounter;
   cout << " number of grade B is :" << bcounter << endl;
   cin>>bcounter;
   cout << " number of grade C is :" << ccounter << endl;
    cin >>ccounter;
    cout << " number of grade D is :" << dcounter << endl;
    cin>>dcounter;
    cout << " number of grade F is :" << fcounter << endl;
    cin>>fcounter;
    
    
    
    return 0;
}


Edit: Removed big text and added code tags. Big text is screaming and please try to use code tags in the future (button with # on it). Thanks smile.gif

This post has been edited by Martyr2: 29 Oct, 2007 - 09:38 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: C++ Help For A Beginner
29 Oct, 2007 - 09:35 PM
Post #11

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
(*edit - Martyr2 fixed up the presentation for the OP - tryin' out the new privileges, I see wink2.gif )

It gives you all of them. It's just not done yet, because it's waiting for you to input something. Why do you have those
cin >> <some_variable>;
statements between outputting the counts of the different letter grades? As an example, the cin >> acounter; statement is used to get user input from the standard input (keyboard), and store the inputted value in acounter. And until you type something in and hit enter, the program will just sit there waiting for you.

If you get rid of those statements, it should be fine.

-jjh

This post has been edited by jjhaag: 29 Oct, 2007 - 10:13 PM
User is offlineProfile CardPM
+Quote Post

harshakirans
RE: C++ Help For A Beginner
29 Oct, 2007 - 09:44 PM
Post #12

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 122



Thanked: 3 times
Dream Kudos: 150
My Contributions
Replace the last few lines with this code..The cin statements are not required after each cout unless desired.

CODE

      cout << " number of grade A is :" << acounter << endl;
      cout << " number of grade B is :" << bcounter << endl;
      cout << " number of grade C is :" << ccounter << endl;
      cout << " number of grade D is :" << dcounter << endl;
      cout << " number of grade F is :" << fcounter << endl;
cin>>acounter;

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 1/7/09 09:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month