C++, Visual Studios: so, code is working except for one bit that i will display
ID: 3875120 • Letter: C
Question
C++, Visual Studios:
so, code is working except for one bit that i will display here. it's really starting to bug me and i'm not sure on now to fix it. please help me. thank you.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void validateUserName(string & _username);
void createFile(string _username);
void writeDataToFile(string _username, string _data);
void readDataFromFile(string _username, int & even, int & odd);
void checkEvenDigit(string _data, int & count);
void checkOddDigit(string _data, int & count);
void displayResults(int even, int odd);
int main()
{
string username;
string line;
int even = 0;
int odd = 0;
cout << "Enter your name and press <ENTER> " << endl;
cin >> username;
validateUserName(username);
createFile(username);
cout << "Enter anything you want and press <ENTER>" << endl;
cout << "To stop entering data, simply type: -1 and press <ENTER>" << endl;
cin >> line;
while (line != "-1")
{
writeDataToFile(username, line);
cout << "Enter anything you want and press <ENTER>" << endl;
cout << "To stop entering data, simply type: -1 and press <ENTER>" << endl;
cin >> line;
}
readDataFromFile(username, even, odd);
displayResults(even, odd);
return 0;
}
/* Here we will validate uaername */
void validateUserName(string & _username)
{
string username;
string line;
string str1;
int counter = 0;
getline(cin, _username);
if (_username(!isalpha))
{
cout << "Invalid entry, try again" << endl;
cin >> _username;
}
else
{
cin >> _username;
cout << endl;
""" + _username;
_username += ".txt"";
}
return;
}
/* Here we are creating file with uaername as name */
void createFile(string _username)
{
ofstream file;
file.open(_username.c_str());
if (!file.is_open())
{
cout << "Cannot open file for writting, exiting ..." << endl;
exit(1);
}
file.close();
return;
}
/* writting data to file */
void writeDataToFile(string _username, string _data)
{
ofstream file;
file.open(_username.c_str());
if (!file.is_open())
{
cout << "Cannot open file for writting, exiting ..." << endl;
exit(1);
}
file << _data << endl;
file.close();
return;
}
/* reading file and counting even and odd */
void readDataFromFile(string _username, int & even, int & odd)
{
string line;
ifstream file;
file.open(_username.c_str());
if (!file.is_open())
{
cout << "Cannot open file for writting, exiting ..." << endl;
exit(1);
}
while (getline(file, line))
{
int count = 0;
checkEvenDigit(line, count);
even = even + count;
count = 0;
checkOddDigit(line, count);
odd = odd + count;
}
file.close();
return;
}
// here we are counting the nu,ber of even digits
void checkEvenDigit(string _data, int & count)
{
int i = 0;
for (i = 0; i < _data.size(); i++)
{
if (isdigit(_data[i]))
{
int x = _data[i] - '0';
if (x % 2 == 0)
count++;
}
}
return;
}
// here we are counting the number of odd digits
void checkOddDigit(string _data, int & count)
{
int i = 0;
for (i = 0; i < _data.size(); i++)
{
if (isdigit(_data[i]))
{
int x = _data[i] - '0';
if (x % 2 != 0)
count++;
}
}
return;
}
void displayResults(int even, int odd)
{
cout << "There are " << even << " even digits in the file your have created " << endl;
cout << "There are " << odd << " odd digits in the file your have created " << endl;
system("pause");
return;
string username string line; string strl; int counter = 0; getline(cin, _username); if (username (!isalpha)) 1 coutExplanation / Answer
Given error is the result of that you are trying to use isalpha in wrong way. Basically isalpha is used to check wether the given _username character is alphabatic or not. Right syntax of isalpha :
int isapha(int c)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.