code doesnt work

whats up guys. im having a lot of problems with my project for my 202 class. its my first time programming in c++ and our teach decided to just throw us into it without much guidance. the programs job is to read in a .txt file which holds a list of names then print the list to the terminal, sort it, then print the sorted list. we arent allowed to use strings or pointers (arrays are allowed). ive gotten everything written but it wont compile and its giving me errors that ive never seen before. IU beleive one error is that the array that i pass into my print function isnt initialized the other is the same error in my sort function ill post my program below ive commented a few funtions out that i am not using currently. thanks for looking and i appreciate the patience as this is probably a bunch of n00b problems

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// project1
// this project alphabetizes a list of names
// written by ****** *****
#include <iostream>
#include <fstream>
using namespace std;

//void read_in_file(char array[]);
//void read_out_file (char array[]);
void print_list(char array[][10]);
void sort_list(char array[][10]);
void swap_lines (char s1[], char s2[]);
int strcmp (char s1[], char s2[], int x);



int main()
{
    char file_name[100];
    char list[10][10];
    int i = 0, j;
    /*ask user for file to open, scans in file the creates object linked to     file*/
    cout << "what file would you like to organize? " << endl;
    cin >> file_name;
    ifstream input_stream;
    input_stream.open (file_name);
    //read file into array
    for (i; i < 10; i++)
    {
        input_stream >> list[i];
    }

    print_list(list);
    sort_list(list);
    print_list(list);
    //create new output file with sorted list inside
    ofstream output_stream;
    output_stream.open ("sorted_list.txt");
    output_stream << list;
    output_stream.close();
    return 0;
}

/*void read_in_file(char array[]);
{
    char list[10][10];
    int i = 0;
    ifstream input_stream;
    input_stream.open (array[]);
    for (i; i < 10; i++)
    {
        input_stream >> list[i];
    }

}*/

void print_list(char array[][10])
{
    int i = 0;
    for (i; i < 10; i++)
    {
        cout << list[i];
    }
}
void sort_list(char array[][10])
{
    /*uses a simple bubble sort algorithm to copare lines of the array and make
    any swaps that are needed*/
    int i,j;
    int flag = 1;
    char s1[10], s2[10];
    for (i=1;(i<10)&&flag;i++)
    {
        flag = 0;
        for(j=0;j<10;j++)
        {
            flag=0;
            int k=strcmp(list[j], list[j+1]);
            if(k==1)
            {
                swap(list[j], list[j+1]);
                flag=1;
            }
        }
    }
}
void swap_lines (char s1[], char s2[])
{
    int i;
    char temp[10];
    for(i=0;i<10;i++)
    {
        temp[i]=s1[i];
        s1[i]=s2[i];
        s2[i]=temp[i];
    }
}
int strcmp (char s1[], char s2[])
{
    int x = 0, i;
    for(i=0;i<10;i++)
    {
        if (s1[i]<s2[i])
        {
            x = -1;
        }
        else if (s1[i]>s2[i])
        {
            x = 1;
        }
        else if (s1[i]==s2[i])
        {
            continue;
        }
        else
        {
            x = 0;
        }
    }
    return x;
}
/*void read_out_file (char array)
{
    ofstream output_stream;
    output_stream.open ("sorted_list.txt");
    output_stream << list;
    output_stream.close;
}*/
Last edited on
Line 21: j is never used.

Line 62,78,81: list is not defined (it's a local in main and not visible here). You should be using array since that is the argument that is passed in.

i was under the impression that for a function you list the parameters then pass an argument of that type into it. is that not true?
That's correct. Arguments have names. In your case, you called the argument array. That's the name you have to use inside the function. A function can not access a local variable inside another function. A function can only access it's own local variables and arguments.
oohhhh gotcha
Topic archived. No new replies allowed.