Output numbers in reverse HELP

Hello,

So I need to write a code that will output numbers in reverse. Which I plan to read into a vector. Here is my code:

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main() {

vector<int>vec;
vector<int>vec2;
vector<int> sortDesc(vector<int> vec);
ifstream infile;

readIn(infile, vector<int> &vec);
print(vector<int> vec);
print(vector<int> vec);

vec2 = sortDesc(vec);

print(vector<int> vec);

return 0;
}

From here I get an error saying:

/Users/marcosortiz/CLionProjects/Ortiz_Lab9/main.cpp:14:32: error: expected '(' for function-style cast or type construction
readIn(infile, vector<int> &vec);

I get this error for my print() functions as well. Any idea on how to fix this would be greatly appreciated. I will also paste my .cpp and .h files

//functions.h
#ifndef ORTIZ_LAB9_FUNCTIONS_H
#define ORTIZ_LAB9_FUNCTIONS_H

#include <vector>
#include <fstream>
using namespace std;
void readIn(ifstream &infile, vector<int> &vec);
void print(vector<int> vec); vector<int> sortDesc(vector<int> vec);

#endif //ORTIZ_LAB9_FUNCTIONS_H

//functions.cpp

#include <vector>
#include <fstream>
#include <iostream>
#include "functions.h"
using namespace std;

void readIn(ifstream &infile, vector<int> &vec){
infile.open("in9.3.txt");
if (infile.is_open()){
while(!infile.eof()){
int x;
infile >> x;
vec.push_back(x);
}
infile.close();
}
}

void print(vector<int> vec){
for(int i=0; i<vec.size(); i++){
cout << vec.at(i) << " ";
}
cout << endl;
}

vector<int> sortDesc(vector<int> vec)
{
for (int i = 0; i < vec.size(); ++i)
{
for (int j = 0; j < vec.size() - i - 1; ++j)
{
if (vec[j] < vec[j + 1]) {
vec[j] = vec[j] + vec[j + 1];
vec[j + 1] = vec[j] - vec[j + 1];
vec[j] = vec[j] - vec[j + 1];
}
}
}
return vec;
}
you have confused function calls and prototypes, best I can tell. prototypes go outside of main, generally above it, for the functions you will define later. Main calls them but not of the format foo(type variable) but only foo(variable). the type does not belong in the call to the function, only in the definition and proto. Look at what I have done here:

try this.. it should be close, but you do need the function bodies for readin & print etc to compile it.
as a side note prefer [] to .at() using vectors unless you NEED at().

(and use code tags <> on the format bar to the side next time please)

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
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

vector<int> sortDesc(vector<int> vec);
readIn(ifstream &infile, vector<int> &vec);
print(vector<int> vec);

int main() {

vector<int>vec;
vector<int>vec2;

ifstream infile;
readIn(infile, vec);
print(vec);

vec2 = sortDesc(vec);

print(vec);

return 0;
}
Last edited on
Yes thank you ( I'm personally still having trouble with functions). So, I edited my code to match what you suggested. The code now compiles successfully but when I run the code nothing is displayed and it returns 0. Which ends the code...

I looked into my print function and everything looks fine (I think)
1
2
3
4
5
6
7
8
void print(vector<int> vec)
{
    for(int i=0; i<vec.size(); i++)
    {
        cout << vec.at(i) << " ";
    }
    cout << endl;
}


My txt file is attached to my code. It's just 6 integers in one line.

Could it be my readIn() function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void readIn(ifstream &infile, vector<int> &vec)
{
    infile.open("in9.3.txt");
    if (infile.is_open())
    {
        while(!infile.eof())
        {
            int x;
            infile >> x;
            vec.push_back(x);
        }
        infile.close();
    }
}
Last edited on
Topic archived. No new replies allowed.