Multiple definition of main without any .cpp file in project folder c++ - c++

I'm trying to build a project which only contains one .cpp file. I'm pretty sure that there is no other files within the folder, but eclipse keep giving me:
multiple definition of `main'
Here is my code:
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <vector>
using namespace std;
int main(){
string input;
vector<double> value;
int count = 0;
while(input != "#") {
cout << "Enter value " << count + 1 << "\n";
cin >> input;
if (input != "#") {
value[count] = atof(input.c_str());
}
count++;
}
cout << count;
double sum = 0;
for (int i = 0; i < count; i++) {
sum += value[i];
}
double ave = sum/count;
double dev = 0;
for (int i = 0; i < count; i++) {
dev += pow((value[i] - ave), 2);
}
dev = sqrt(dev / (count - 1));
cout << "\nThe average is " << ave << "\n";
cout << "The standard deviation is" << dev << "\n";
return 0;
}
Any help would be appreciated.

The comments are saying that your code is fine (I haven't tested it myself, though) so look around at your project directory, and see if there are any source files that shouldn't be there. If push comes to shove you can just copy/paste your code into a new project.

Related

How to read more than one line of input?

So I have built a small basic data encrypter (for learning purposes only). It is working perfectly fine but it reads only a single line of input. Is it my Editor problem or my code have some issues.
ps: I use CodeBlocks
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
std::string str;
char enc;
int word;
cout << "\t\t\t\t\t\t\t\tENCRYPTOR" <<endl;
cout << "\t\t\t\t\t\t\t\t---------" <<endl;
cout << "Enter a Word: ";
getline(cin, str);
int n = 0;
cout << "\n\n\t\t\t\t\t\t\t\tENCRYPTED D#T#" <<endl;
cout << "\t\t\t\t\t\t\t\t--------------\n\n" << endl;
for(int i = 0; i < str.length(); i++){
int randomAdd[5] = {5,6,2,3,2};
int size = sizeof(randomAdd)/sizeof(randomAdd[0]);
// for(int j = 0; j < 5; j++){
word = str.at(i);
if(i%5 == 0){
n = 0;
}
enc = int(word) + randomAdd[n];
std::cout << char(enc);
n++;
}
return 0;
}
This works
Hello World
But I cannot enter this
Hello World
Have a nice day
because then the program exits command prompt without any error or message.
How can I read more than one line?
You can do as
#include <iostream>
using namespace std;
int main() {
string str;
while (getline(cin, str)) {
cout << str << endl;
}
return 0;
}
This code sample allows you to input multiple lines interactively from the command line/shell
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str;
char enc;
int word;
vector<string> myInput;
cout << "\t\t\t\t\t\t\t\tENCRYPTOR" <<endl;
cout << "\t\t\t\t\t\t\t\t---------" <<endl;
while (str != "Enigma")
{
cout << "Enter a line (Write Enigma to exit input): ";
getline(cin, str);
myInput.push_back(str);
}
int n = 0;
cout << "\n\n\t\t\t\t\t\t\t\tENCRYPTED D#T#" <<endl;
cout << "\t\t\t\t\t\t\t\t--------------\n\n" << endl;
for(auto & myInputLine : myInput)
{
str = myInputLine;
for (size_t i = 0; i < str.length(); i++) {
int randomAdd[5] = { 5,6,2,3,2 };
int size = sizeof(randomAdd) / sizeof(randomAdd[0]);
word = str.at(i);
if (i % 5 == 0) {
n = 0;
}
enc = int(word) + randomAdd[n];
std::cout << char(enc);
n++;
}
}
return 0;
}
The input is finished if Enigma is written.
All input is stored in the vector container of the STL, see vector.
Afterwards, all the lines are encrypted by your algorithm.
Hope it helps?

C++ rand() not working with string array

I'm creating a small program that allows the user to input 3 names (or whatever string they want). The program should then display all three strings (which is working), then it should use the rand() function to randomly display one of the three strings. This is the part that isn't functioning properly.
#include <iostream>
#include <string>
using namespace std;
void display(string[], int);
const int SIZE = 3;
int main()
{
string names[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout << i + 1 << ": ";
getline(cin, names[i]);
}
cout << endl;
display(names, SIZE);
int name = rand() % (2 + 1 - 0) + 0;
cout << names[name];
cin.get();
return 0;
}
void display(string nm[], int n)
{
int i = 0;
for (i; i < n; i++)
{
cout << "Name " << i + 1 << ": ";
cout << nm[i] << endl;
}
}
I had it set up differently before, and it gave me an error, but after changing it to what it is now, it always gives me the last element [2].
Is this a code error, or is it just that rand() always gives the same output on the same system?
After some discussion in the comments, it became apparent that the issue was that I was not seeding the rand() function. Below is part of the code that was not functioning, corrected.
(Also, as a sidenote, to use the time() function, <ctime> or <time.h> has to be included.)
srand(time(NULL));
int name = rand() % 3;
cout << names[name];
(Thanks to #manni66 for pointing out that it was useless to include an overly complicated calculation to get the range for rand(), as it just had to be a single integer.
seeding with current time works :
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;
void display(string[], int);
const int SIZE = 3;
int main()
{
string names[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout << i + 1 << ": ";
getline(cin, names[i]);
}
cout << endl;
display(names, SIZE);
srand(time(NULL)); // use current time as seed for random generator
int name = rand() % 3 ;
printf(" random %i \n", name);
cout << names[name];
cin.get();
return 0;
}
void display(string nm[], int n)
{
int i = 0;
for (i; i < n; i++)
{
cout << "Name " << i + 1 << ": ";
cout << nm[i] << endl;
}
}

C++ endless cin console prompt

I've made a simple program which is supposed to ask the user for the length of a set, fill it with numbers and find the minimal value of that set. When I run the code, program works fine until the last number of the set is entered. Console prompt keeps blinking but it doesn't react to the keyboard. The program stops at this point. I don't understand why it doesn't just stop asking for input. I'm using CodeBlocks 16.01 if that matters. Here is is the source code:
#include <iostream>
using namespace std;
int main()
{
int len;
cout << "How many elements?" << endl;
cin >> len;
int myset[len];
int temp;
cout << "Enter " << len <<" numbers: " << endl;
for (int x = 0; x < len; x++)
{
cin >> temp;
myset[x] = temp;
cout << endl;
}
int mini;
for (int i = 0; i < len; i++)
{
if (i = 0)
{
mini = myset[i];
}
else if(myset[i] < mini)
{
mini = myset[i];
}
}
cout << "Minimal value of this set: " << mini << endl;
}
You set i to 0 in the if(i = 0) line... I suppose you want to write "if (i==0)"

c++ program stop working with atof()

This is my first program on C++. I successfully build it. When I run it, Windows keep giving me program is stop working, the same result that I try to run it with eclipse.
Here is my code:
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <vector>
using namespace std;
int main(){
string input;
vector<double> value;
int count = 0;
while(input != "#") {
cout << "Enter value " << count + 1 << "\n";
cin >> input;
cout << input;
if (input != "#") {
value[count] = atof(input.c_str());
}
count++;
}
cout << count;
double sum = 0;
for (int i = 0; i < count; i++) {
sum += value[i];
}
double ave = sum/count;
double dev = 0;
for (int i = 0; i < count; i++) {
dev += pow((value[i] - ave), 2);
}
dev = sqrt(dev / (count - 1));
cout << "\nThe average is " << ave << "\n";
cout << "The standard deviation is" << dev << "\n";
return 0;
}
Anyone has any idea? Thank you.
value[count] = atof(input.c_str());
is a problem since value does not have enough space in it. Use
value.push_back(atof(input.c_str()));
instead.
You also have a logic error in the while loop. count will be incremented even when the input is "#". I recommend changing it to:
while(true) {
cout << "Enter value " << count + 1 << "\n";
cin >> input;
cout << input;
if (input == "#") {
break;
}
value.push_back(atof(input.c_str()));
}
count = value.size();
I tried the code on other's computer. It works great. I think something goes wrong for my compiler.

Main never runs? Bubble sort & Selections sort

I can't seem to have any output and unfortunately my teacher can't help me. I'm using xcode and previously we have had to change quite a few things to get programs to work and this time I cannot get anything to happen. At first I had just a bubble sort with six numbers and got it to run, but we then added a selection sort and instead of having numbers, they are read in from a txt file.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
void bubble(string filename);
void selectionSort(string filename);
const int SIZE = 100000;
int values[SIZE];
// Main
int main()
{
cout << "Beginning set of our bubble sorts. Please stand by... " << endl;
bubble("inOrder.txt");
bubble("revOrder.txt");
bubble("ranOrder1.txt");
bubble("ranOrder2.txt");
cout << "Finished with four bubble sorts. " << endl;
system("PAUSE");
return 0;
}
// Functions
void bubble(string filename)
{
unsigned long int passCount = 0, compCount = 0, swapCount = 0;
int temp;
bool swap;
ifstream fin(filename.c_str());
for ( int j = 0; j < SIZE; j++)
fin >> values[j];
do
{ passCount++;
swap = false;
for (int count = 0; count < (SIZE -1); count++)
{
compCount++;
if(values[count] > values[count + 1])
{
swapCount++;
temp = values[count];
values[count] = values[count + 1];
values[count + 1] = temp;
swap = true;
}
}
} while (swap);
string outputName = "Bubble" + filename;
ofstream fout(outputName.c_str());
for ( int j = 0; j < SIZE; j++)
fout << values[j] << endl;
cout << "The number of passes is: " << passCount << endl;
cout << "The number of comparious is:" << compCount << endl;
cout << "The number of swaps is: " << swapCount << endl;
}
// Selection
void selectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (SIZE - 1); startScan++)
{
minIndex = startScan;
minValue = values[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (values[index] < minValue)
{
minValue = values[index];
minIndex = index;
}
}
values[minIndex = array[startScan]];
values[startScan] = minValue;
}
}
I tried changing the word 'array' in the sorts to values but it didn't work. And before changing the parameters of the function (and changing the other parts mentioned above) we only had 'string filename' and it worked fine. A lot of things are working for the teacher, but she is using Visual Express. I'm downloading that now but the school internet is extremely slow so I'm trying to work with what I have.
Any suggestions would be greatly appreciated.
Also, the most of the code was done as a class, but not everything worked the same in xcode. As homework I had to add the selection sort and I'm struggling a bit.
EDIT
I'm sorry, I meant I need the output to be files. We had a seperate .cpp file to generate the number needed for the program to do the sorts.
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
const int maxSize = 100000;
int main()
{
ofstream fout1("inOrder.txt");
ofstream fout2("revOrder.txt");
ofstream fout3("ranOrder.txt");
ofstream fout4("ranOrder.txt");
cout << "Starting to create 4 output files. . ." << endl;
for (int i = 1; i <= maxSize; i++)
{
fout1 << i << endl;
fout2 << maxSize - i + 1 << endl;
fout3 << rand() << endl;
fout4 << rand() << endl;
}
cout << "Finished creating 4 output files . . ." << endl;
system( "PAUSE");
return 0;
}
All of this code was written by my instructor. For Xcode do I just add the file to the same folder as the main program?
I used to have this issue with opening files using Xcode as well. I've found that if you don't specify a full file path for the txt file you are trying to open, Xcode will check the project folder as expected. The thing is that you have to have added the txt file to the Xcode project, not just drop it in the project folder in Finder.
In Xcode, go to to FILE > Add Files and add the txt files you are trying to open to the project.