Set is not removing the duplicates [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am having trouble putting a file of words into a set. I can read the file and the words go into the set but the set doesn't discard the repeated words. Here is the snippet of code that I believe is causing the problem.
using namespace std;
while(readText >> line){
set<string> wordSet;
wordSet.insert(line);
for (std::set<std::string>::iterator i = wordSet.begin(); i != wordSet.end(); i++)
{
cout << *i << " ";
}
}
the sample file is this
1
2
2
3
4
5
5
and the output is exactly the same

As stated in comments, you are not using the std::set correctly. You need to move it, and the for loop, outside of your while loop:
using namespace std;
set<string> wordSet;
while(readText >> line) {
wordSet.insert(line);
}
for (set<string>::iterator i = wordSet.begin(); i != wordSet.end(); i++) {
cout << *i << " ";
}

Related

compiler issues with simple array program that wont build due to limitations in scope perhaps [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
this is my first question on stack overflow.. Im having compiler issues and wanted to figure out if anyone could help me find out why.
this is the unfortunate error message I recieve every time I attempt to compile.
//*************************************************************************
// This program uses array with a for loop control structure to prompt the user to enter their most convenient days of the week to work
#include <iostream>
using namespace std;
#include <array>
#include <cstddef>
int main() {
int whichDay = 0;
array< string, 7> daysOfweek =
{
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"Friday",
"saturday"
};
for (size_t x= 0; x< daysOfweek.size(); x++)
{
cout << daysOfWeek[x] << endl;
};
cout << "enter your work day: " ;
cin >> whichDay;
cout << daysOfWeek[whichDay] += 1
return 0;
}
This is your issue: cout << daysOfWeek[whichDay] += 1.
What would you expect the result of "monday" += 1 to be?
You probably just want to display the chosen day, so you just need:
cout << daysOfWeek[whichDay];
Well, you also may want to check that the input value is in the valid range :)

How to fix input and output problem with files in C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I can't find a problem with my code. It's loading in DEV C++ but after that the window with "Program has stopped working" pop up.
fstream file;
file.open("dane1.txt");
string linia;
string tab[5];
int i = 0;
do
{
getline(file,linia);
cout<<linia<<endl;
tab[i]=linia;
i++;
}
while(!file.eof());
file.close();
ofstream file2("wynik.txt");
if (file2)
{
for(int i=5;i>0;i--)
{
file2<< tab[i];
file2<< endl;
}
}
else
{
cout<<"You have problem with file!"<<endl;
}
pliki.close();
I want to get lines from 1st file (dane1.txt) and then put it in diffrent order in fail "wyniki.txt"
string tab[5];
// ...
for(int i=5;i>0;i--)
{
file2<< tab[i];
file2<< endl;
}
The first iteration of this for loop attempts to access tab[5] which, of course, doesn't exist since the five-element tab array contains only tab[0] through tab[4]. Undefined behavior, and the near-certain reason for your program crashing.

Use for loop to write to many files in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to write data to a 100 files gnu_2D_t0_Q_xt_2sol_anal.dat, gnu_2D_t1_Q_xt_2sol_anal.dat, ..., gnu_2D_t100_Q_xt_2sol_anal.dat. Using this question as inspiration, I've come up with the following snippet of code
int m = 100;
for(int k=0;k<m;k++)
{
stringstream aa; // http://www.dreamincode.net/forums/topic/95826-stringstream-tutorial/
aa << k;
filename = "gnu_2D_t" + aa.str() + "_Q_xt_2sol_anal.dat";
files.open(filename.c_str()); // http://www.cplusplus.com/forum/beginner/122208/
for(int i=0;i<SPACE;i=i+1)
{
files << x_begin + (i * h) << setw(18);
files << Q_xt_matrix[i][t_matrix[m]] << setw(18);
files << endl;
}
files.close;
}
However, it yields the following error
error: invalid use of non-static member function files.close;
^
Does anybody what is wrong with my code?
close is a method, not a member of files. Change the last line to files.close().

Unterminating while loop in c++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm trying to store values in double array in a while loop with a terminating value of -1. It seems the values are being stored fine but when -1 is entered the loop does not terminate. Any ideas? Thank you so much!
screen shot of the problem area
int main(void)
{
double a[7],dev[6],mean, std;
int i,n;
int entered;
char letg[6];
cout<<"Please enter the test grades one at a time (max 6)\n";
cout<<"enter a -1 when you are done entering scores\n";
//based off class notes
i=0;
cin>>entered;
while (entered>0)
{ a[i]=entered;
i++;
cin>>entered;
}
Change
{while (entered>0)
to
while (entered>0) {
Your process is executing the statement a[i]=entered; indefinitely. You should put the braces after the while loop:
while (entered>0)
{
a[i]=entered;
i++;
cin>>entered;
}

C++ ? Why the value does not change ? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I just learned how to use this operator : ->,
and I am trying to create practice programs so I can remember it and be familiar with it.
I created a program that inputs my health and then heal my (add health) using the -> operator.
But when I run program, my health stays at 50 (cause I set my current health to 50).
here is my code :
#include <iostream>
using namespace std;
struct myhealth
{
unsigned short my_health;
};
void addhealth(myhealth* addhealth)
{
addhealth->my_health += 50;
};
int main()
{
myhealth player;
player.my_health = 50;
cout << "My earlier health : " << player.my_health << endl;
myhealth();
cout << "My current health : " << player.my_health;
cin.get()
return 0;
}
You never call addhealth and so the value is never modified.
This line of code:
myhealth();
appears to have been written in error. Instead I think you meant to write:
addhealth(&player);