Question

The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

The code will not run and I get the following errors in Visual Studio. Please fix the errors.

error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>'
cpp(32): error C2228: left of '.open' must have class/struct/union
(32): note: type is 'int'
): error C2065: 'cout': undeclared identifier
error C2065: 'cout': undeclared identifier
error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>'
error C2440: 'initializing': cannot convert from 'const char [68]' to 'int'
note: There is no context in which this conversion is possible
error C2065: 'cout': undeclared identifier
error C2065: 'cout': undeclared identifier
error C2672: 'getline': no matching overloaded function found
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 studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\string(160): note: see declaration of 'std::getline'

#include<iostream>
#include<fstream>
#include<string.h>
#include "stdafx.h"
#include <string>


using namespace std;

const int SIZE = 200;


int mostPopular(string names[], string name);

int mostPopular(string popularNames[], string name, int n) {

   for (int i = 0; i<n; i++) {
       if (name == popularNames[i])
           return 1;
   }
   return 0;
}


int main() {

   string boys[SIZE];
   string girls[SIZE];

   string girlName, boyName;
   int i = 0, j = 0;

   ifstream boyInputFile("BoyNames.txt");

   if (boyInputFile) {
       cout << "Boys file opened successfully\n";
   }
   else {
       cout << "Error opening file BoyNames.txt!\n";

   }

   ifstream girlInputFile("GirlNames.txt");

   if (girlInputFile) {
       cout << "Girls file opening successfully\n";
   }
   else {
       cout << "Error opening file GirlNames.txt!\n";
   }

   while (getline(boyInputFile, boys[i])) {
       i++;
   }
   while (getline(girlInputFile, girls[j])) {
       j++;
   }

   boyInputFile.close();
   girlInputFile.close();

   cout << "\nEnter a boy's name: ";
   getline(cin, boyName);

   cout << "\nEnter a girl's name: ";
   getline(cin, girlName);

   if (mostPopular(boys, boyName, i) == 1) {
       cout << boyName << " is a most popular boy name\n";
   }
   else {
       cout << boyName << " is not a most popular name\n";
   }

   if (mostPopular(girls, girlName, j) == 1) {
       cout << girlName << " is a most popular name\n";
   }
   else {
       cout << girlName << " is not a most popular girl name\n";
   }

   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

#include<fstream>

#include<string.h>

#include <string>

using namespace std;

const int SIZE = 200;

int mostPopular(string names[], string name);

int mostPopular(string popularNames[], string name, int n) {

   for (int i = 0; i<n; i++) {

       if (name == popularNames[i])

           return 1;

   }

   return 0;

}

int main() {

   string boys[SIZE];

   string girls[SIZE];

   string girlName, boyName;

   int i = 0, j = 0;

   ifstream boyInputFile("BoyNames.txt");

   if (boyInputFile) {

       cout << "Boys file opened successfully\n";

   }

   else {

       cout << "Error opening file BoyNames.txt!\n";

   }

   ifstream girlInputFile("GirlNames.txt");

   if (girlInputFile) {

       cout << "Girls file opening successfully\n";

   }

   else {

       cout << "Error opening file GirlNames.txt!\n";

   }

   while (getline(boyInputFile, boys[i])) {

       i++;

   }

   while (getline(girlInputFile, girls[j])) {

       j++;

   }

   boyInputFile.close();

   girlInputFile.close();

   cout << "\nEnter a boy's name: ";

   getline(cin, boyName);

   cout << "\nEnter a girl's name: ";

   getline(cin, girlName);

   if (mostPopular(boys, boyName, i) == 1) {

       cout << boyName << " is a most popular boy name\n";

   }

   else {

       cout << boyName << " is not a most popular name\n";

   }

   if (mostPopular(girls, girlName, j) == 1) {

       cout << girlName << " is a most popular name\n";

   }

   else {

       cout << girlName << " is not a most popular girl name\n";

   }

   return 0;

}


Sample Output

Error opening file BoyNames.txt! Error opening file GirlNames.txt! 6 Enter a boys name: Chandler Enter a girls name: Phoebe

Add a comment
Know the answer?
Add Answer to:
The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Getting the following errors and not sure why. My classes for Van are defined just like...

    Getting the following errors and not sure why. My classes for Van are defined just like for everything else. Also not sure why I am getting the error "undeclared identifier" . Below are the errors when I try and build: ______________________________________________________________________________________ ./vehicles.h:1:2: error: unterminated conditional directive #ifndef VEHICLES_H ^ vehicles.cpp:33:44: error: member initializer 'verbose_' does not name a non-static data member or base class : serialNumber_(0), passengerCapacity_(0), verbose_(false)                                            ^~~~~~~~~~~~~~~ vehicles.cpp:36:10: error: out-of-line definition of 'Vehicle' does not match any...

  • (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

    (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.            **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...

  • my code deosn't run and it doesn't show any error. what's wrong about it !! it...

    my code deosn't run and it doesn't show any error. what's wrong about it !! it should loads data from a text file into an array of structs and prints the array to the screen here is the code: #include <iostream> #include <string> #include <fstream> using namespace std; struct Company {    string Departmentname;    int Departmentnum; }; const int MAX = 20; int main() { ifstream File;    Company arrayofdepartment[MAX];    int Departmentcount ;      File.open("comapny.txt");       if...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • I am getting a bunch of errors. Really need help for C Programming using Microsoft Visual....

    I am getting a bunch of errors. Really need help for C Programming using Microsoft Visual. Error Codes listed below code. Thank you. #define _CRT_SECURE_NO_WARNINGS #include #include #include typedef struct { int *array; int effectiveSize; int maximumSize; } DynamicArray; void CreateArray(DynamicArray *pDynamicStruct, int initialSize) { pDynamicStruct->array = (int *)malloc(sizeof(int) * initialSize); void ExpandArray(DynamicArray *pDynamicStruct); { DynamicArray pDynamicStructNew = (int *)malloc(sizeof(int) * pDynamicStruct->maximumSize * 2); for (int i = 0l i < effectiveSize; i++); pDynamicStructNew->array[i] = pDynamicStruct->array[i]; } //3) Free the...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT