|
-
May 31st, 2012, 09:56 AM
#1
Algorithm problem
I have a algorithm bulit but I have errors and don't know why I am getting them.
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
typedef std::vector <std::string> StringArray;
int main()
{
cout << "This program is a database for storing information." << endl;
ofstream myfile;
myfile. open ("data.txt", ios::app);
cout << "Please input what you want to store." << endl;
StringArray s;
std::string inString;
for (int i = 0, i < 5; i++ ;); /// line 17
s.push_back(inString);
{ getline (cin, inString);
myfile << inString << endl;
}
sort (s.begin(), s.end(), greater<int>());
myfile. close ();
system("pause");
return 0;
}
I get the errors
In function `int main()':|
|17|error: expected init-declarator before '<' token|
|17|error: expected `,' or `;' before '<' token|
stl_algo.h|2319|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
C:\MinGW\include\c++\3.4.2\bits\stl_function.h|218|note: candidates are: bool std::greater<_Tp>:: operator()(const _Tp&, const _Tp&) const [with _Tp = int]|
stl_algo.h||In function `const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Compare = std::greater<int>]':|
stl_algo.h|124|error: no match for call to `(std::greater<int>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
stl_algo.h|125|error: no match for call to `(std::greater<int>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
stl_algo.h|127|error: no match for call to `(std::greater<int>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
stl_algo.h|131|error: no match for call to `(std::greater<int>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
stl_algo.h|133|error: no match for call to `(std::greater<int>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
stl_algo.h|2041|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
stl_algo.h|2044|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
stl_algo.h|2145|error: no match for call to `(std::greater<int>) (std::string&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
stl_algo.h|2093|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
stl_heap.h|279|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
stl_heap.h|166|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
What is going on here? I've been researching this for a couple days and can't figure out whats going wrong.
-
May 31st, 2012, 10:27 AM
#2
Re: Algorithm problem
Code:
for (int i = 0, i < 5; i++ ;); /// line 17
Check your formatting there.
you probably want
Code:
for (int i = 0; i < 5; i++ ) /// line 17
-
May 31st, 2012, 10:27 AM
#3
Re: Algorithm problem
Code:
for (int i = 0, i < 5; i++ ;);
Shouldn't that be?
Code:
for (int i = 0; i < 5; i++ )
-
May 31st, 2012, 10:29 AM
#4
Re: Algorithm problem
for (int i = 0, i < 5; i++ ; ); /// line 17
that was to solve another error I was getting saying
|17|error: expected `;' before ')' token|
-
May 31st, 2012, 10:30 AM
#5
Re: Algorithm problem
 Originally Posted by smurf12125
that was to solve another error I was getting saying
|17|error: expected `;' before ')' token|
But it's not correct syntax for a for statement.
-
May 31st, 2012, 10:34 AM
#6
Re: Algorithm problem
 Originally Posted by GCDEF
you probably want
Code:
for (int i = 0; i < 5; i++ ) /// line 17
That just is leading me to more errors and expecting more primary expressions before every semicolon
-
May 31st, 2012, 10:36 AM
#7
Re: Algorithm problem
 Originally Posted by GCDEF
But it's not correct syntax for a for statement.
here is the full error when I remove that semicolon
C:\Documents and Settings\indstdy\Desktop\database.cpp|17|error: expected `;' before ')' token|
-
May 31st, 2012, 10:36 AM
#8
Re: Algorithm problem
 Originally Posted by smurf12125
That just is leading me to more errors and expecting more primary expressions before every semicolon
Then you have other problems. That's syntactically correct. Are you sure that's exactly how you have it? Paste your exact code with that syntax.
The only error I get is it doesn't know what greater is.
Last edited by GCDEF; May 31st, 2012 at 10:39 AM.
-
May 31st, 2012, 10:41 AM
#9
Re: Algorithm problem
You were right there was an comma I accidentally didn't delete so I fixed that now all the rest of the errors are saying that happen in the algo.h file but here is my code now
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
typedef std::vector <std::string> StringArray;
int main()
{
cout << "This program is a database for storing information." << endl;
ofstream myfile;
myfile. open ("data.txt", ios::app);
cout << "Please input what you want to store." << endl;
StringArray s;
std::string inString;
for (int i = 0; i < 5; i++ );
s.push_back(inString);
{ getline (cin, inString);
myfile << inString << endl;
}
sort (s.begin(), s.end(), greater<int>());
myfile. close ();
system("pause");
return 0;
}
-
May 31st, 2012, 10:45 AM
#10
Re: Algorithm problem
Hi,
Although I'm not sure what you are attempting to do next line is wrong:
Code:
for (int i = 0, i < 5; i++ ;); /// line 17
It should be
Code:
for (int i = 0; i < 5; i++);
Anyway, above expression is useless because of ';' at the end (out of parenthesis). It just iterates 5 times doing nothing else.
There's another compiler mistake. StringArray is a vector of strings. You sorting there as if they were a vector of ints?
Code:
sort (s.begin(), s.end(), greater<int>());
Hope this helps.
Albert.
Please, correct me. I'm just learning.... and sorry for my english :-)
-
May 31st, 2012, 10:50 AM
#11
Re: Algorithm problem
 Originally Posted by AlbertGM
There's another compiler mistake. StringArray is a vector of strings. You sorting there as if they were a vector of ints?
Code:
sort (s.begin(), s.end(), greater<int>());
Hope this helps,
Albert.
Wouldn't I need to since StringArray would repeat until it a break function is input? Because StringArray would keep inputing text to the .txt file and would clear itself when it inputs the text to the file.
-
May 31st, 2012, 11:17 AM
#12
Re: Algorithm problem
 Originally Posted by smurf12125
Wouldn't I need to since StringArray would repeat until it a break function is input? Because StringArray would keep inputing text to the .txt file and would clear itself when it inputs the text to the file.
Not following that one. Wouldn't you need to what?
-
May 31st, 2012, 11:23 AM
#13
Re: Algorithm problem
 Originally Posted by smurf12125
Wouldn't I need to since StringArray would repeat until it a break function is input? Because StringArray would keep inputing text to the .txt file and would clear itself when it inputs the text to the file.
It could be because of my terrible english, but what this has to do with your compiler errors? I don't get it.
Albert.
Please, correct me. I'm just learning.... and sorry for my english :-)
-
May 31st, 2012, 02:23 PM
#14
Re: Algorithm problem
 Originally Posted by smurf12125
Wouldn't I need to since StringArray would repeat until it a break function is input? Because StringArray would keep inputing text to the .txt file and would clear itself when it inputs the text to the file.
Point to us where you were told that std::sort did anything that you stated. Or are you just making things up?
std::sort does that -- sorts a sequence of values. It doesn't do anything else. The last argument depends on the first two -- you are sorting std::string, not int, therefore the greater template must be based on std::string.
Regards,
Paul McKenzie
-
June 1st, 2012, 11:29 AM
#15
Re: Algorithm problem
 Originally Posted by Paul McKenzie
Point to us where you were told that std::sort did anything that you stated. Or are you just making things up?
std::sort does that -- sorts a sequence of values. It doesn't do anything else. The last argument depends on the first two -- you are sorting std::string, not int, therefore the greater template must be based on std::string.
Regards,
Paul McKenzie
I need a break function to end the loop of inputting my text not for std::sort and Albert was talking about how I was sorting it as a vecor of ints instead of a vector of strings. I asked since I was using StringArray that I would need to have them input as as int's instead of strings. Here is my code now so you can see what I mean.
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
typedef std::vector <std::string> StringArray;
int main()
{
cout << "This program is a database for storing information." << endl;
ofstream myfile;
myfile. open ("data.txt", ios::app);
cout << "Please input what you want to store." << endl;
StringArray s;
std::string inString;
for (int i = 0; i < 5; i++ )
s.push_back(inString);
{ getline (cin, inString);
myfile << inString << endl;
}
std::sort (s.begin(), s.end(), greater<int>());
myfile. close ();
system("pause");
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|