I need help with some errors when trying to build a program. 1>------ Build star
ID: 3877451 • Letter: I
Question
I need help with some errors when trying to build a program.
1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>assignment1searchandsort.cpp
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(18): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(19): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(19): error C3861: 'setw': identifier not found
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(21): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(21): error C3861: 'setw': identifier not found
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(23): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(23): error C2563: mismatch in formal parameter list
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(23): error C2568: '<<': unable to resolve function overload
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(23): note: could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(89): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(90): error C2065: 'cin': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(92): error C2079: 'infile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>'
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(92): error C2440: 'initializing': cannot convert from 'char [50]' to 'int'
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(92): note: There is no context in which this conversion is possible
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(93): error C2228: left of '.is_open' must have class/struct/union
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(93): note: type is 'int'
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(95): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(103): error C2672: 'getline': no matching overloaded function found
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(103): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'int'
1>c:program files (x86)microsoft visual studio7communityc oolsmsvc.11.25503includestring(155): note: see declaration of 'std::getline'
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(103): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided
1>c:program files (x86)microsoft visual studio7communityc oolsmsvc.11.25503includestring(144): note: see declaration of 'std::getline'
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(103): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'int'
1>c:program files (x86)microsoft visual studio7communityc oolsmsvc.11.25503includestring(124): note: see declaration of 'std::getline'
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(103): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided
1>c:program files (x86)microsoft visual studio7communityc oolsmsvc.11.25503includestring(71): note: see declaration of 'std::getline'
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(105): error C2228: left of '.close' must have class/struct/union
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(105): note: type is 'int'
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(111): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(112): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(113): error C2065: 'cin': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(128): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(129): error C2065: 'cin': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(133): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(134): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(135): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(136): error C2065: 'cin': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(142): error C2065: 'cout': undeclared identifier
1>c:userszosource eposconsoleapplication1consoleapplication1ssignment1searchandsort.cpp(144): error C2065: 'cout': undeclared identifier
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This is the code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include "stdafx.h"
#include <string>
using namespace std;
void display(string message, string words[], int n);
void selectionSort(string words[], int n);
void bubbleSort(string words[], int n);
int binarySearch(string words[], int n, string search);
int linearSearch(string words[], int n, string search);
void display(string message, string words[], int n)
{
cout << message << endl;
cout << left << setw(12) << "Index" << setw(30) << "Word" << endl;
for (int i = 0; i < n; i++)
cout << left << setw(12) << i << setw(30) << words[i] << endl;
cout << endl << endl;
}
void selectionSort(string words[], int n)
{
int minIdx;
string temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (words[j] < words[minIdx])
minIdx = j;
}
if (i != minIdx) //swap?
{
temp = words[i];
words[i] = words[minIdx];
words[minIdx] = temp;
}
}
}
void bubbleSort(string words[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (words[j] > words[j + 1]) //compare 2 consecutive locations and swap if needed
{
string temp = words[j];
words[j] = words[j + 1];
words[j + 1] = temp;
}
}
}
}
int binarySearch(string words[], int n, string search)
{
int low = 0, high = n - 1;
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if (words[mid] == search)
return mid;
else if (search < words[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;//when not found
}
int linearSearch(string words[], int n, string search)
{
for (int i = 0; i < n; i++)
{
if (words[i] == search)
return i;
}
return -1; //when not found
}
int main()
{
char filename[50];
cout << "Enter input filename: ";
cin >> filename;
ifstream infile(filename);
if (!infile.is_open())
{
cout << "Error, could not open: " << filename << endl;
return 1;
}
//put the strings into the array
string words[50];
int n = 0;
while (getline(infile, words[n]))
n++;
infile.close();
display("Words before sorting", words, n);
int choice;
cout << "You may enter 1 to selection sort, or 2 to bubble sort." << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1)
selectionSort(words, n);
if (choice == 2)
bubbleSort(words, n);
display("Words after sorting", words, n);
string search = "";
int index;
while (search != "stop")
{
cout << "Enter a word to search(type stop to stop): ";
cin >> search;
if (search != "stop")
{
cout << "1. Linear search" << endl;
cout << "2. Binary search" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1)
index = linearSearch(words, n, search);
else
index = binarySearch(words, n, search);
if (index == -1)
cout << search << " NOT found" << endl << endl;
else
cout << search << " found at index " << index << endl << endl;
}
}
}
It will compile fine on other compilers like https://www.onlinegdb.com/online_c++_compiler.
Explanation / Answer
stdafx.h shall be the first include directive in your source file, because Visual Studio uses a precompiled header, so we need to include stdafx.h before any other directive
Now it should work fine
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void display(string message, string words[], int n);
void selectionSort(string words[], int n);
void bubbleSort(string words[], int n);
int binarySearch(string words[], int n, string search);
int linearSearch(string words[], int n, string search);
void display(string message, string words[], int n)
{
cout << message << endl;
cout << left << setw(12) << "Index" << setw(30) << "Word" << endl;
for (int i = 0; i < n; i++)
cout << left << setw(12) << i << setw(30) << words[i] << endl;
cout << endl << endl;
}
void selectionSort(string words[], int n)
{
int minIdx;
string temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (words[j] < words[minIdx])
minIdx = j;
}
if (i != minIdx) //swap?
{
temp = words[i];
words[i] = words[minIdx];
words[minIdx] = temp;
}
}
}
void bubbleSort(string words[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (words[j] > words[j + 1]) //compare 2 consecutive locations and swap if needed
{
string temp = words[j];
words[j] = words[j + 1];
words[j + 1] = temp;
}
}
}
}
int binarySearch(string words[], int n, string search)
{
int low = 0, high = n - 1;
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if (words[mid] == search)
return mid;
else if (search < words[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;//when not found
}
int linearSearch(string words[], int n, string search)
{
for (int i = 0; i < n; i++)
{
if (words[i] == search)
return i;
}
return -1; //when not found
}
int main()
{
char filename[50];
cout << "Enter input filename: ";
cin >> filename;
ifstream infile(filename);
if (!infile.is_open())
{
cout << "Error, could not open: " << filename << endl;
return 1;
}
//put the strings into the array
string words[50];
int n = 0;
while (getline(infile, words[n]))
n++;
infile.close();
display("Words before sorting", words, n);
int choice;
cout << "You may enter 1 to selection sort, or 2 to bubble sort." << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1)
selectionSort(words, n);
if (choice == 2)
bubbleSort(words, n);
display("Words after sorting", words, n);
string search = "";
int index;
while (search != "stop")
{
cout << "Enter a word to search(type stop to stop): ";
cin >> search;
if (search != "stop")
{
cout << "1. Linear search" << endl;
cout << "2. Binary search" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1)
index = linearSearch(words, n, search);
else
index = binarySearch(words, n, search);
if (index == -1)
cout << search << " NOT found" << endl << endl;
else
cout << search << " found at index " << index << endl << endl;
}
}
}