|
-
June 1st, 2012, 11:39 AM
#16
Re: Algorithm problem
 Originally Posted by smurf12125
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;
}
There is rhyme and reason to formatting. Do it right and code is much easier to understand. Just correctly formatting your code points out a pretty obvious error with the for statement and the lines that follow.
Your conversation about strings and ints still doesn't make sense.
Last edited by GCDEF; June 1st, 2012 at 11:41 AM.
-
June 1st, 2012, 11:46 AM
#17
Re: Algorithm problem
 Originally Posted by GCDEF
There is rhyme and reason to formatting. Do it right and code is much easier to understand. Just correctly formatting your code points out a pretty obvious error with the for statement and the lines that follow.
Your conversation about strings and ints still doesn't make sense.
When I compile I don't get any errors in my for statement I keep getting this as my errors
C:\MinGW\include\c++\3.4.2\bits\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> >&)'|
and your right I don't know what I was even thinking i was trying to sort int's instead of strings.
-
June 1st, 2012, 03:15 PM
#18
Re: Algorithm problem
You don't need that param at all. Just remove it an sort will sort the strings in ascending order.
-
June 2nd, 2012, 02:22 PM
#19
Re: Algorithm problem
 Originally Posted by GCDEF
There is rhyme and reason to formatting. Do it right and code is much easier to understand. Just correctly formatting your code points out a pretty obvious error with the for statement and the lines that follow.
 Originally Posted by smurf12125
When I compile I don't get any errors in my for statement I keep getting this as my errors
...
GCDEF doesn't mean the for statement has a compile error - he means it has a logical error. I.e. it isn't doing what you think it is.
In your code:
Code:
std::string inString;
for (int i = 0; i < 5; i++ )
s.push_back(inString);
{
getline (cin, inString);
myfile << inString << endl;
}
I have highlighted all the code which is part of the for loop in red. The remainder is executed once the loop is finished. Is this what you thought the code means? I would guess not.
-
June 5th, 2012, 11:30 AM
#20
Re: Algorithm problem
that's just to loop the program to keep repeating until it has 5 inputs. What is the statement doing then?
Last edited by smurf12125; June 5th, 2012 at 11:35 AM.
-
June 5th, 2012, 11:37 AM
#21
Re: Algorithm problem
It's nothing wrong with it except that it does push_back 5 times with an empty string. Check where the braces are placed.
-
June 5th, 2012, 11:43 AM
#22
Re: Algorithm problem
oops had an unnecessary pair of braces there.
-
June 5th, 2012, 11:46 AM
#23
Re: Algorithm problem
No you don't. Just as GCDEF said the code does probably not doing what you want it to do.
-
June 5th, 2012, 11:49 AM
#24
Re: Algorithm problem
 Originally Posted by S_M_A
It's nothing wrong with it except that it does push_back 5 times with an empty string. Check where the braces are placed.
He explained what it was doing and wow not thinking clearly today I just need to move my braces around my for statement and keep the other brace after my statement that has my input statement.
-
June 5th, 2012, 11:51 AM
#25
Re: Algorithm problem
You probably want to move the push_back line as well.
-
June 5th, 2012, 11:52 AM
#26
Re: Algorithm problem
 Originally Posted by smurf12125
He explained what it was doing and wow not thinking clearly today  I just need to move my braces around my for statement and keep the other brace after my statement that has my input statement.
You probably want to actually get the string before you add it to s also.
-
June 5th, 2012, 11:53 AM
#27
Re: Algorithm problem
 Originally Posted by smurf12125
oops had an unnecessary pair of braces there.
No. This is the syntax for a multi-line for loop:
Code:
for (initializer; condition; postcondition)
{
// everything goes here that falls into the loop.
}
I don't know why you say you have unnecessary braces, when what's in the braces determines what is being looped. If you thought that mere indenting the code under the for() did anything, it does absolutely nothing.
Code:
std::string inString;
for (int i = 0; i < 5; i++ )
s.push_back(inString);
getline (cin, inString);
myfile << inString << endl;
If this is what you thought meant "I want to loop those three lines of code for 5 iterations", well C++ doesn't work by indents. The only line that falls in the loop in the above example is the push_back line. The indented lines below that are just that -- indented. Indentation doesn't play any part in how C++ works, only how your code looks.
Regards,
Paul McKenzie
-
June 5th, 2012, 11:55 AM
#28
Re: Algorithm problem
 Originally Posted by GCDEF
Repeating what? Explain in English what you want that for loop to accomplish.
The loop is supposed to make five copys of InString for the five different inputs that are going to input into the "database"
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
|