By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,054 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,054 IT Pros & Developers. It's quick & easy.

displaying number of students in each course

P: 2
I'm using c++(language)
i need to display number of students doing each course, seemingly it displays the first number well but the second time i try it adds all the students doing all courses. here's the code below:
Expand|Select|Wrap|Line Numbers
  1.     {
  2.                     string str;
  3.                     int n, count = 0;
  4.  
  5.                 cout<<"Enter number of courses: ";
  6.                 cin>>n;
  7.  
  8.             for(int i=0; i<=n-1; i++)
  9.                     {
  10.                     cout<<"Enter course: ";
  11.                 cin>>str;
  12.  
  13.          for(int i=0; i<=str.size(); i++)    
  14.  
  15.          {         
  16.  
  17.          if(str == stud[i].cours)
  18.  
  19.          {
  20.  
  21. count++;
  22.  
  23. cout<<"Number of students doing "<<str<<" are "<<count<<endl;
  24.  
  25. }
  26.  
  27. }
  28.  
  29.  
  30. }
  31.  
  32. }
2 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


dev7060
100+
P: 117
From the snippet you have provided, it seems that a single count variable is being used. Different count variable needs to be used for each course to display distinct results.

Also, only the count++ part should be present in the body of if. After the if block, count can be printed.
5 Days Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.