Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having some difficulties with my C++ code (Note: Code is incomplete) but I

ID: 3867788 • Letter: I

Question

I am having some difficulties with my C++ code (Note: Code is incomplete) but I keep getting a "warning C4700" error when running it in my compiler. I know it has to do with main() and GetColumnValue(). Here it my code:                      


#include <shlwapi.h>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;

enum COLUMNINDEX {THEME, TITLE, ARTIST, YEAR, SPOTIFYURL};
enum COLUMNINDEX GetColumnIndex(string ColumnSearch);


void GetColumnValue(string ColumnValue, int DisplayIndex);
string GetSpotifyURL();
void LaunchSpotify();

int main() {
string ColumnSearch;
string ColumnValue;
int DisplayIndex;
COLUMNINDEX Column;
char userAccessKey;
ifstream inFile;
string Matched;

//Attempt to open file
inFile.open("1000songs.csv");
if (inFile.fail())
{
  cout << "Error loading files";
  return 1;
}

cout << "Welcome please press any key besides 'x' to start" << endl;
cin >> userAccessKey;
//While loop that will continue until user enters X
while (userAccessKey != 'X') {
  cout << "Please enter the column to search (Theme, Title, Artist, or Year): ";
  cin >> ColumnSearch;
  transform(ColumnSearch.begin(), ColumnSearch.end(), ColumnSearch.begin(), ::toupper);
  cout << "Please enter the value in the '" << ColumnSearch << "' column to locate: ";
  cin >> ColumnValue;
  transform(ColumnValue.begin(), ColumnValue.end(), ColumnValue.begin(), ::toupper);
  cout << "Please enter the maximum number of records to display per page (1 to 100): " << endl;
  cin >> DisplayIndex;
  cout << endl;
  cout << "...Searching column " << ColumnSearch << " for the item '" << ColumnValue << "'." << endl;

  GetColumnIndex(ColumnSearch);

  if (Column == THEME) {
   GetColumnValue(ColumnValue, DisplayIndex);
   cout << endl;
  }
  else if (Column == TITLE) {
   GetColumnValue(ColumnValue, DisplayIndex);
   cout << endl;
  }
  else if (Column == ARTIST) {
   GetColumnValue(ColumnValue, DisplayIndex);
   cout << endl;
  }
  else if (Column == YEAR) {
   GetColumnValue(ColumnValue, DisplayIndex);
   cout << endl;
  }
  else {
   cout << "Invalid choice" << endl;
   exit(0);
  }

  cout << "Press any key to continue searching or 'x' to stop..." << endl;
  cin >> userAccessKey;
  toupper(userAccessKey);


}
return 0;
}


enum COLUMNINDEX GetColumnIndex(string ColumnSearch)
{
COLUMNINDEX Column;
if (ColumnSearch == "THEME") {
  Column = THEME;
}
else if (ColumnSearch == "TITLE") {
  Column = TITLE;
}
else if (ColumnSearch == "ARTIST"){
  Column = ARTIST;
}
else if (ColumnSearch == "YEAR") {
  Column = YEAR;
}
else {
  cout << "Invalid choice" << endl;
  exit(0);
}
return Column;
}
void GetColumnValue(string ColumnValue, int DisplayIndex) {
COLUMNINDEX Column;
ifstream inFile;
string Theme, Title, Artist, Year;
int count = 0;
string iTheme[100], iTitle[100], iArtist[100], iYear[100];
int i = 0;
int unMatched = 0;
int Matched = 0;

inFile.open("1000songs.csv");
if (inFile.fail())
{
  cout << "Error loading files";
  exit(0);
}

if (Column == THEME) {
  while (!inFile.eof()) {
   inFile >> Theme;
   if (Theme == ColumnValue) {
    iTheme[i] = Theme;
    iTitle[i] = Title;
    iArtist[i] = Artist;
    iYear[i] = Year;
    i = i + 1;
    Matched = Matched + 1;
   }
   else {
    unMatched = unMatched + 1;
    
   }
  }

}
else if (Column == TITLE) {
  

}
else if (Column == ARTIST) {
  

}
else if (Column == YEAR) {
  

}
else {
  cout << "Invalid choice" << endl;
  exit(0);
}
while (i <= DisplayIndex) {
  cout << "Rec No. " << i << endl;
  cout << "Title: " << Title[i] << endl;
  cout << "Artist: " << Artist[i] << endl;
  cout << "Year: " << Year[i] << endl;
}
cout << Matched << " records found." << endl;

}

Explanation / Answer

You were getting the error since you did not assign any value to Column variable. You need to assign the return value fo GetColumnIndex() to Column and use that value in other function. I have corrected the code and also added functionality to extract the values from file (comma separated values). The output is shown below. If the answer helped, please don't forget to rate it. Thank you.

#include <shlwapi.h>
#include <iostream>
#include <fstream>
#include <cstring>

#include <algorithm>
#include <ctime>
using namespace std;
enum COLUMNINDEX {THEME, TITLE, ARTIST, YEAR, SPOTIFYURL};
enum COLUMNINDEX GetColumnIndex(string ColumnSearch);
void ExtractValues(string line, string delimiter, string &Theme, string &Title, string &Artist, string &Year, string &URL);
void GetColumnValue(COLUMNINDEX Column, string ColumnValue, int DisplayIndex);
string GetSpotifyURL();
void LaunchSpotify();
int main() {
string ColumnSearch;
string ColumnValue;
int DisplayIndex;
COLUMNINDEX Column;
char userAccessKey;
ifstream inFile;
string Matched;
//Attempt to open file
inFile.open("/Users/raji/Documents/Chegg/c++/Test/Test/1000songs.csv");
if (inFile.fail())
{
cout << "Error loading files";
return 1;
}
cout << "Welcome please press any key besides 'x' to start" << endl;
cin >> userAccessKey;
//While loop that will continue until user enters X
while (userAccessKey != 'X' && userAccessKey != 'x') {
cout << "Please enter the column to search (Theme, Title, Artist, or Year): ";
cin >> ColumnSearch;
transform(ColumnSearch.begin(), ColumnSearch.end(), ColumnSearch.begin(), ::toupper);
cout << "Please enter the value in the '" << ColumnSearch << "' column to locate: ";
cin >> ColumnValue;
transform(ColumnValue.begin(), ColumnValue.end(), ColumnValue.begin(), ::toupper);
cout << "Please enter the maximum number of records to display per page (1 to 100): " << endl;
cin >> DisplayIndex;
cout << endl;
cout << "...Searching column " << ColumnSearch << " for the item '" << ColumnValue << "'." << endl;
Column = GetColumnIndex(ColumnSearch);
if (Column == THEME) {
GetColumnValue(Column, ColumnValue, DisplayIndex);
cout << endl;
}
else if (Column == TITLE) {
GetColumnValue(Column, ColumnValue, DisplayIndex);
cout << endl;
}
else if (Column == ARTIST) {
GetColumnValue(Column, ColumnValue, DisplayIndex);
cout << endl;
}
else if (Column == YEAR) {
GetColumnValue(Column, ColumnValue, DisplayIndex);
cout << endl;
}
else {
cout << "Invalid choice" << endl;
exit(0);
}
cout << "Press any key to continue searching or 'x' to stop..." << endl;
cin >> userAccessKey;
toupper(userAccessKey);
  
}
return 0;
}

enum COLUMNINDEX GetColumnIndex(string ColumnSearch)
{
COLUMNINDEX Column;
if (ColumnSearch == "THEME") {
Column = THEME;
}
else if (ColumnSearch == "TITLE") {
Column = TITLE;
}
else if (ColumnSearch == "ARTIST"){
Column = ARTIST;
}
else if (ColumnSearch == "YEAR") {
Column = YEAR;
}
else {
cout << "Invalid choice" << endl;
exit(0);
}
return Column;
}
  

void ExtractValues(string line, string delimiter, string &Theme, string &Title, string &Artist, string &Year, string &URL)
{
  
long idx1 = 0, idx2 = line.find(delimiter);
  
Theme = line.substr(idx1, idx2-idx1);
  
idx1 = idx2 + delimiter.length();
idx2 = line.find(delimiter, idx1);
Title = line.substr(idx1, idx2-idx1);
  
idx1 = idx2 + delimiter.length();
idx2 = line.find(delimiter, idx1);
Artist = line.substr(idx1, idx2-idx1);
  
idx1 = idx2 + delimiter.length();
idx2 = line.find(delimiter, idx1);
Year = line.substr(idx1, idx2-idx1);

idx1 = idx2 + delimiter.length();
URL = line.substr(idx1);
}
  
  
void GetColumnValue(COLUMNINDEX Column,string ColumnValue, int DisplayIndex) {
  
ifstream inFile;
string Theme, Title, Artist, Year, URL;

string iTheme[100], iTitle[100], iArtist[100], iYear[100];
int i = 0;
int unMatched = 0;
int Matched = 0;
inFile.open("/Users/raji/Documents/Chegg/c++/Test/Test/1000songs.csv");
if (inFile.fail())
{
cout << "Error loading files";
exit(0);
}
  
string line;
bool match;
while (true) {
getline(inFile, line);
if(inFile.eof())
break;
  
transform(line.begin(), line.end(), line.begin(), ::toupper);

ExtractValues(line, ",", Theme, Title, Artist, Year, URL);
match = false;
if(Column == THEME && Theme == ColumnValue)
match = true;
else if(Column == TITLE && Title == ColumnValue)
match = true;
else if (Column == ARTIST && Artist == ColumnValue)
match = true;
else if(Column == YEAR && Year == ColumnValue)
match = true;
  
if(match)
{
//cout << line << endl;
Matched = Matched + 1;
if(i < DisplayIndex)
{
iTheme[i] = Theme;
iTitle[i] = Title;
iArtist[i] = Artist;
iYear[i] = Year;
i = i + 1;
}
}
else {
unMatched = unMatched + 1;
  
}
}
inFile.close();
  
for(i = 0; i < Matched && i < DisplayIndex; i++) {
cout << "Rec No. " << (i+1) << endl;
cout << "Title: " << iTitle[i] << endl;
cout << "Artist: " << iArtist[i] << endl;
cout << "Year: " << iYear[i] << endl << endl;
}
cout << Matched << " records found." << endl;
}

output

Welcome please press any key besides 'x' to start
d
Please enter the column to search (Theme, Title, Artist, or Year): Theme
Please enter the value in the 'THEME' column to locate: love
Please enter the maximum number of records to display per page (1 to 100):
10

...Searching column THEME for the item 'LOVE'.
Rec No. 1
Title: THE LOOK OF LOVE
Artist: ABC
Year: 1982

Rec No. 2
Title: THE SHINING
Artist: BADLY DRAWN BOY
Year: 2000

Rec No. 3
Title: GOD ONLY KNOWS
Artist: THE BEACH BOYS
Year: 1966

Rec No. 4
Title: GOOD VIBRATIONS
Artist: THE BEACH BOYS
Year: 1966

Rec No. 5
Title: WOULDN?T IT BE NICE
Artist: THE BEACH BOYS
Year: 1966

Rec No. 6
Title: EIGHT DAYS A WEEK
Artist: THE BEATLES
Year: 1964

Rec No. 7
Title: GIRL
Artist: THE BEATLES
Year: 1965

Rec No. 8
Title: I WANT TO HOLD YOUR HAND
Artist: THE BEATLES
Year: 1963

Rec No. 9
Title: SHE LOVES YOU
Artist: THE BEATLES
Year: 1963

Rec No. 10
Title: SOMETHING
Artist: THE BEATLES
Year: 1969

139 records found.

Press any key to continue searching or 'x' to stop...
d
Please enter the column to search (Theme, Title, Artist, or Year): Year
Please enter the value in the 'YEAR' column to locate: 1966
Please enter the maximum number of records to display per page (1 to 100):
15

...Searching column YEAR for the item '1966'.
Rec No. 1
Title: GOD ONLY KNOWS
Artist: THE BEACH BOYS
Year: 1966

Rec No. 2
Title: GOOD VIBRATIONS
Artist: THE BEACH BOYS
Year: 1966

Rec No. 3
Title: WOULDN?T IT BE NICE
Artist: THE BEACH BOYS
Year: 1966

Rec No. 4
Title: SIT DOWN I THINK I LOVE YOU
Artist: BUFFALO SPRINGFIELD
Year: 1966

Rec No. 5
Title: VISIONS OF JOHANNA
Artist: BOB DYLAN
Year: 1966

Rec No. 6
Title: ELUSIVE BUTTERFLY
Artist: BOB LIND
Year: 1966

Rec No. 7
Title: FLY ME TO THE MOON
Artist: FRANK SINATRA
Year: 1966

Rec No. 8
Title: GET ME TO THE CHURCH ON TIME
Artist: FRANK SINATRA
Year: 1966

Rec No. 9
Title: YOU CAN?T HURRY LOVE
Artist: THE SUPREMES
Year: 1966

Rec No. 10
Title: DO I LOVE YOU (INDEED I DO)
Artist: FRANK WILSON
Year: 1966

Rec No. 11
Title: JUST LIKE A WOMAN
Artist: BOB DYLAN
Year: 1966

Rec No. 12
Title: SHE?S YOUR LOVER NOW
Artist: BOB DYLAN
Year: 1966

Rec No. 13
Title: STUPID GIRL
Artist: THE ROLLING STONES
Year: 1966

Rec No. 14
Title: UNDER MY THUMB
Artist: THE ROLLING STONES
Year: 1966

Rec No. 15
Title: WHAT BECOMES OF THE BROKEN HEARTED
Artist: JIMMY RUFFIN
Year: 1966

35 records found.

Press any key to continue searching or 'x' to stop...
x