Question

Can someone please edit my current code to accept a user input for the text file...

Can someone please edit my current code to accept a user input for the text file name in lieu of how it currently will read data1.txt. Also if the user enters a data file that isn't ready to be used, cout a statement "cout << "Could not open file " << userInputforData.txt << endl;

Another condition that I need to add is if there is a special character anywhere in the word it cannot be an acceptable variable name.

could you also add a count function that ive already declared for the total of valid variable names.

Below is my current c++ code:

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
void printReport(string userText, int vowels, int consonents, int specialCharacters, bool isValid);
bool isVowel(char c);
//void getCount(string userText, int& vowels, int& consonents, int& numbers, int& specialCharacters);
bool isValid(char t);
int main()
{
    ifstream inFile("data1.txt");
    string s;
    char ch;
    int vowels = 0, consonents = 0, specialCharacters = 0, numbers = 0;
    while(inFile >> s) {
        vowels = consonents = specialCharacters = numbers = 0;
        for (int i = 0; i < static_cast<int>(s.length()); i++) {
            ch = s[i];
            if(i==0) {
                if ((ch >= 'a' && ch <= 'z'))
                    printReport(s, vowels, consonents, specialCharacters, true);
                else{
                    printReport(s, vowels, consonents, specialCharacters, false);
                }
            }
            if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
                if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' ||
                    ch == 'I' || ch == 'O' || ch == 'U') {
                    vowels += 1;
                } else {
                    consonents += 1;
                }
            } else if (!(ch >= '0' && ch <= '9')) {
                specialCharacters += 1;
            } else if (ch >= '0' && ch <= '9') {
                numbers += 1;
            }
        }
      //  cout << s <<  " " <<vowels << " " << consonents << endl;

    }
   // printReport(s, vowels, consonents, specialCharacters);
    isVowel(ch);
    inFile.close();
    return 0;
}
/*void getCount(string userText, int& vowels, int& consonents, int& numbers, int& specialCharacters);{

}*/
void printReport(string s, int vowels, int consonents, int specialCharacters, bool isValid) {
    if (isValid) {
        cout << "The variable name - " << s <<  " - is valid." << endl;
    }
    else {
        cout << "The variable name - " << s << " - is invalid." << endl;
    }
}
bool isVowel(char ch){
    if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){
        return true;
    }else{
        return false;
    }
}

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

code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
void printReport(string userText, int vowels, int consonents, int specialCharacters, bool isValid);
bool isVowel(char c);
//void getCount(string userText, int& vowels, int& consonents, int& numbers, int& specialCharacters);
bool isValid(char t);
bool hasSpecialCharacters(string);

int main()
{
string fileName;
cout <<"Enter a file Name: "<<endl;
cin >> fileName;

ifstream inFile(fileName.c_str());
if (!inFile.is_open()) {
cout << "Could not open file : " << fileName << endl;
exit(1);
}
string s;
char ch;
int vowels = 0, consonents = 0, specialCharacters = 0, numbers = 0;
while(inFile >> s) {
vowels = consonents = specialCharacters = numbers = 0;
if (!hasSpecialCharacters(s)){
for (int i = 0; i < static_cast<int>(s.length()); i++) {
ch = s[i];
if(i==0) {
if ((ch >= 'a' && ch <= 'z'))
printReport(s, vowels, consonents, specialCharacters, true);
else{
printReport(s, vowels, consonents, specialCharacters, false);
}
}
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' || ch == 'U') {
vowels += 1;
} else {
consonents += 1;
}
} else if (!(ch >= '0' && ch <= '9')) {
specialCharacters += 1;
} else if (ch >= '0' && ch <= '9') {
numbers += 1;
}
}
// cout << s << " " <<vowels << " " << consonents << endl;
}
else {
printReport(s, vowels, consonents, specialCharacters, false);
}

}
// printReport(s, vowels, consonents, specialCharacters);
isVowel(ch);
inFile.close();
return 0;
}
/*void getCount(string userText, int& vowels, int& consonents, int& numbers, int& specialCharacters);{

}*/
void printReport(string s, int vowels, int consonents, int specialCharacters, bool isValid) {
if (isValid) {
cout << "The variable name - " << s << " - is valid." << endl;
}
else {
cout << "The variable name - " << s << " - is invalid." << endl;
}
}
bool isVowel(char ch){
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){
return true;
}else{
return false;
}
}

bool hasSpecialCharacters(string str) {
// 48 - 57, 65 - 90, 97 - 122, or 95
for (int i=0;i<str.length();i++) {
if ((str[i] >='0' && str[i]<='0') || (str[i] >='A' && str[i]<='Z') || (str[i] >='a' && str[i]<'z') || str[i] == '_') {
continue;
}
else {
return true;
}
}
return false;
}

output:

Add a comment
Know the answer?
Add Answer to:
Can someone please edit my current code to accept a user input for the text file...
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
  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • 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];...

  • Please Update my C++ code: Implement array to replace for vector: Example --> input: 100 output:...

    Please Update my C++ code: Implement array to replace for vector: Example --> input: 100 output: A 1000Pairs.txt looks like this: {100, A } {200, A } {300, B } {400, C } {500, D } {600, E } {700, F } {800, G } {900, H } {1000, I } {1100, J } {1200, K } {1300, L } {1400, M } {1500, N } {1600, O } {1700, P } {1800, Q } {1900, R } {2000, S...

  • I'm having trouble getting my program to output this statement Enter a sentence: Test! There are...

    I'm having trouble getting my program to output this statement Enter a sentence: Test! There are 5 total characters. There are 1 vowel. There are 1 UPPERCASE letters. There are 3 lowercase letters. There are 1 other characters. Here's my code: #include<string.h> #include<stdio.h> #include<stdbool.h> int main() { char str[100]; int i; int vowels=0; int UC; int LC; int Others; int c; printf("Enter a sentence: \n"); gets(s); LC=countLC(&s); UC=countUC(&s); Others=countOthers(&s); printf("There are %d total characters.\n", ; for(i=0; i<strlen(str); i++){ if(isVowel(str[i])) vowels++;...

  • Hi! 1. I need some help with sorting string in a text file. My goal is...

    Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...

  • 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>...

  • My code doesn't output correctly using a .txt file. How do I clean this up so...

    My code doesn't output correctly using a .txt file. How do I clean this up so it posts correctly? ----------------------------------------------- CODE ---------------------------------------------- #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <fstream> using namespace std; struct Customer {    int accountNumber;    string customerFullName;    string customerEmail;    double accountBalance; }; void sortDesc(Customer* customerArray, int size); void print(Customer customerArray[], int size); void print(Customer customerArray[], int size) {    cout << fixed << setprecision(2);    for (int i = 0; i...

  • 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);...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • I wrote code in C++ that takes a text file containing information on different football players...

    I wrote code in C++ that takes a text file containing information on different football players and their combine results, stores those players as a vector of objects of the class, and prints out those objects. Finally, I wrote a function that calculates the average weight of the players. MAIN.CPP: #include "Combine.h" #include using namespace std; // main is a global function // main is a global function int main() { // This is a line comment //cout << "Hello,...

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