limiting number of characters to be displayed..

can anyone help me with this...
i want to output only 25 characters(name and course) in this program..but its display all the inputted characters.

here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;

int main()
{
    int numStud ;
    double score, numScore, sum = 0;
    double  ave;
    string name, course;
    
    getline(cin,name);
    getline(cin,course);
    cin >> numScore;
    for(int i = 0; i < numScore; i++)
    {
       cin >> score;
       sum = sum + score;
       
    }
    ave = sum / numScore;
    
    printf("NAME\t\t\tCOURSE\t\t\tSCORE\n");
    printf("%s\t\t%s\t\t%.2f\n", name.c_str(), course.c_str(), ave);
    system("pause");
    return 0;
}



this is my input:
Supercalifragelisticexpialidocious Abracadabra
BS Digital Illustration and Animation 
10 9 10 2 4 0 9 8 10 9 7


any help will be highly appreciated...thank you in advance :)
Since you are using std::string why not use C++ streams and std::string.substr()?

1
2
3
cout << "NAME\t\t\tCOURSE\t\t\tSCORE\n";
cout << name.substr(0,25) << "\t\t" << course.substr(0,25) << endl;
cout << ave << endl;
yeah..I forgot about substr()... anyway, thank you very much jib for the help. I really appreciate your help :)


additional question..

i've come up with this code..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<string>
#include<cstdlib>
#include<vector>
using namespace std;


int main()
{
    string name = "", course="", x;
    int numStud;
    
    vector <double> s;
    double score, sum = 0, ave, numScore;
    
    cin >> numStud;
    for(int j = 0; j < numStud; j++)
    {
      for(int i = 1; i <= 2; i++)
      {
        getline(cin,x);
        if(i == 1)
          name = x;
        else 
          course = x;   
      
      }
      cin >> numScore;
      for(int t = 0; t < numScore; t++)
      {
         cin >> score;
         s.push_back(score);
         sum = sum + score;
      }
      ave  = sum / numScore;
      printf("NAME\t\t\t\tCOURSE\t\t\t\tSCORE\n");
      printf("%s\t%s\t%.2f\n",(name.substr(0,25)).c_str(), (course.substr(0,25)).c_str(), ave);
    }
    
    
    return 0;
}


i want to loop the input and the output of this program but there is something wrong with this....

here is my input

3
Mickey Mouse
BS Psychology
10 6 2 1 1 0 6 10 10 8 9
Supercalifragelisticexpialidocious Abracadabra
BS Business Administration 
10 9 10 2 4 0 9 8 10 9 7
Jar Jar Binks
BS Information Technology
10 8 9 3 9 1 6 7 8 5 5 5


and this should be the output

NAME                      COURSE                    SCORE
Supercalifragelisticexpia BS Business Administratio  6.80
Jar Jar Binks             BS Information Technology  6.10
Mickey Mouse              BS Psychology              5.30
Last edited on
Why are you mixing C-stdio output functions and C++ streams? You should pick one or the other. For C++ programs you should be using C++ streams, not the C-stdio functions for most occasions.

What output are you getting?

Topic archived. No new replies allowed.