Scoreboard problem

Hi, im having troubles with my scoreboard, i should input names and racetimes for each name, and it should rearenge it from best to last. But this dont work, please help me

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
43
44
45
46
47
48
49
  #include <stdio.h>
    #include <conio.h>
    #include <string>
    
    void main()
    
    {
    int time[10],ttime,i,j;
    std::string name[10],tname;
    
    for (i=0;i<10;i++)
    {
    printf("Name: ");
    scanf("%s\n",&name[i]);
    printf("\n");
    printf(" Time: ");
    scanf("%d\n",&time[i]);
    
    }
    
    //sorting bubble
    
    for (i=0;i<10;i++)
    {
    for (j=i+1;j<10;j++)
    {
    if (time[i]<time[j])
    {
    ttime = time[i];
    time[i]= time[j];
    time[j]=ttime;
    
    tname=name[i];
    name[i]=name[j];
    name[j]=tname;
    
    }
    
    }
    
    
    }
    for(i=0;i<10;i++)
    {
    printf("%s --- %d10\n",name[i],time[i]);
    }
    getch();
    
    }
scanf("%s\n",&name[i]); scanf won't take a std::string as an argument.
use cin.(and include iostream)
C stream fuctions and std::string don't mix very well.Use c++ streams (cout , cin)
for instance , do this:
1
2
3
4
std::string a[10];
std::cin >> a[0];//input a word
std::getline(cin,a[1]);//input a line
std::cout << a[0] << a[1] <<std::endl;

juzernejm wrote:
1
2
3
4
5
#include <conio.h>
...
void main()
...
getch();


Please don't use conio.h and getch().They are not standard and are non-portable.
Please don't use void main,The standard usage is :
int main() , or int main(int argc,char* argv[])
Last edited on
Do not use C I/O with C++ classes.
If you need several parallel arrays, use single array and struct instead.
Do not reinvent the wheel.
conio.h was deprecated in the previous century. Do not use it.
void main() is illegal in C++
Declare variables just before you would use them and limit their scope.

Correct way to do your 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
#include <algorithm>
#include <iostream>
#include <string>

struct record
{
    int time;
    std::string name;
};

bool operator<(const record& lhs, const record& rhs)
{
    return lhs.time < rhs.time;
}

int main()
{
    record records[10];
    for (int i=0; i<10; ++i) {
        std::cout << " Name: ";
        std::cin >> records[i].name;
        std::cout << "\n Time: ";
        std::cin >> records[i].time;
    }
    std::sort(records, records + 10);
//  std::sort(records, records + 10, [](const record& lhs, const record& rhs){return lhs.time < rhs.time;});
    for(int i=0; i<10; ++i)
       std::cout << records[i].name << " --- " << records[i].time << '\n';
}   
Last edited on
Topic archived. No new replies allowed.