Can someone help me debug this program - c++

Here is my code thus far, I think its doing its job right and i believe the problem is with my arrays.
#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;
const char FileName[] = "text.txt";
int main()
{
string line;
ifstream inMyStream(FileName);
int c;
int upperCaseCount[26] = { 0 };
int lowerCaseCount[26] = { 0 };
char oneLetter;
if (inMyStream.is_open())
{
while (getline(inMyStream, line))
{
c += line.length();
}
for (unsigned n = 0; n < line.length(); ++n)
{
oneLetter = char(line[n]);
if (oneLetter >= 'A' && oneLetter <= 'Z')
{
upperCaseCount[int(oneLetter) - 'A']++;
}
else if (oneLetter >= 'a' && oneLetter <= 'z')
{
lowerCaseCount[int(oneLetter) - 'a']++;
}
}
}
inMyStream.close();
cout << "Uppercase Characters: " << upperCaseCount << endl;
cout << "Lowercase Characters: " << lowerCaseCount <<endl;
cout << "Digits: " << c << endl;
return 0;
}
Here's the output I'm getting--->
C:\Users\House\Desktop\Lab11>test
Uppercase Characters: 0x28fcf4
Lowercase Characters: 0x28fd5c
Digits: 1959532772
C:\Users\House\Desktop\Lab11>

I see two issues (there is another big one that I left for you to debug).
You need to initialize the variable c to some useful starting value (perhaps 0).
You need to iterate over your arrays to print their contents. The odd looking outputs you currently get are addresses, not the array contents.

Here is a Working example for counting upperCases lowerCases and numbers
#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;
const char FileName[] = "text.txt";
int main()
{
string line;
ifstream inMyStream(FileName);
int c=0;//counts digits
int upperCount=0;//counts uppercases
int lowerCount=0;//counts lowercases
char oneLetter;
if (inMyStream.is_open())
{
while (getline(inMyStream, line))
{
for (unsigned n = 0; n < line.length(); ++n)
{
oneLetter = line[n];
if (oneLetter >= 'A' && oneLetter <= 'Z')
{
upperCount++;
}
else if (oneLetter >= 'a' && oneLetter <= 'z')
{
lowerCount++;
}
else if (oneLetter >= '0' && oneLetter <= '9'){
c++;
}
}
}
}
inMyStream.close();
cout << "Uppercase Characters: " << upperCount << endl;
cout << "Lowercase Characters: " << lowerCount <<endl;
cout << "Digits: " << c << endl;
return 0;
}
hope it helped ;)

Related

s.erase is not working when trying to remove spaces from a string

I am trying to remove the spaces from a string to validate a Palindrome phrase. I have looked up other methods, but my professor literally copy and pasted the remove space for loop in our instructions but I can't get it to work and he says he doesn't want us going to the internet for help. I am trying to remove spaces from a phrase like "too hot to hoot" to validate it. I can get my program to work with single words like "bob", but not phrases.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char input[100];
cout << "Please enter a word/phrase: ";
cin >> input;
for (int i = 0; i < strlen(input); i++)
{
while (s[i] == ' ')//getting "s" is undefined error
s.erase(i,1);
}
int i = 0;
int j = strlen(input)-1;
bool a = true;
for (i = 0; i < j; i++)
{
if (input[i] != input[j])
{
a = false;
}
j--;
}
if(a)
{
cout << input << " is a Valid Palindrome." << endl;
}
else
{
cout<< input << " is not a Valid Palindrome." << endl;
}
system("pause");
return 0;
}
Maybe you have not copy the result from temporary variable 's'. So, the modified codes should be:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
char input[100];
cout << "Please enter a word/phrase: ";
fgets(input, 100, stdin);
string s(input); // define a temporary variable 's'
int i = 0;
while (i < s.length())
{
if (s[i] == ' ' || s[i] == '\n')
{
s.erase(i, 1); // erase from variable 's', other then 'input'
continue;
}
i++;
}
// copy result from 's' to 'input'
sprintf(input, "%s", s.c_str());
int j = strlen(input) - 1;
bool a = true;
i = 0;
for (i = 0; i < j; i++)
{
if (input[i] != input[j])
{
a = false;
}
j--;
}
if (a)
{
cout << input << " is a Valid Palindrome." << endl;
}
else
{
cout << input << " is not a Valid Palindrome." << endl;
}
system("pause");
return 0;
}

How to not print the last comma of a list of integers? [duplicate]

This question already has answers here:
How can I print a list of elements separated by commas?
(34 answers)
Closed 6 years ago.
This is when going through a list of integers in order seperated by commas and I only print one instance of an integer even where there are more than one seperated by commas. (CodeEval challenge https://www.codeeval.com/open_challenges/29/)
My problem is I am trying to do this in linear time without any external storage. And I can't have a comma at the end (e.g. 1,3,4,6,). The solutions I found online all use some list to store the integers and then they do a print.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string str = "1,2,2,3,3,3,4,4";
char c;
int num = -1;
for (int i = 0; i < str.length(); ++i) {
if (str[i] == ',') continue;
else {
c = str[i];
if ((c - '0') != num) {
num = c - '0';
cout << num << ",";
}
}
}
cout << endl;
return 0;
}
One of the solution is to use boolean flag:
bool first = true;
for( ... ) {
if( first ) first = false;
else std::cout << ',';
std::cout << data;
}
if (i == str.length() - 1)
{
cout << num;
}
else
{
count << num << ",";
}
Or you could print a backspace at the end of the string processing:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string str = "1,2,2,3,3,3,4,4";
char c;
int num = -1;
for (int i = 0; i < str.length(); ++i) {
if (str[i] == ',') continue;
else {
c = str[i];
if ((c - '0') != num) {
num = c - '0';
cout << num << ",";
}
}
}
cout << '\b';
cout << endl;
return 0;
}

program in c++ that only reads numeric values of a string variable. but not working

I tried to create a program in c++ that should only reads numeric values of a string variable. But it does not seem to work. Can anyone tell me why?
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "11111111111";
for(unsigned i = 0; i<=str.length(); i++) {
if(str.at(i)!='0' || str.at(i)!='1' ||str.at(i)!='2' ||str.at(i)!='3' ||
str.at(i)!='4' || str.at(i)!='5' ||str.at(i)!='6' ||str.at(i)!='7' ||
str.at(i)!='8' || str.at(i)!='9' ||
str.at(i)!='-' ) {
cout << "Invalid Phone Number!" << endl;
cout << str.at(i);
break;
}
}
cout << str;
}
You could use regex:
#include <regex>
std::string str = "11111111111";
if (false == std::regex_match(str, std::regex("[-0-9]+")))
{
std::cout << "Invalid Phone Number!\n";
}
for(unsigned i = 0; i<=str.length(); i++)
change the comparison to i < str.length(), and fix the logic:
for (unsigned i = 0; i < str.length(); i++)
{
if (str[i] < '0' || str[i] > '9')
{
if (str[i] != '-')
{
cout << str[i] << "\n";
cout << "Invalid Phone Number!" << endl;
break;
}
}
}

Print the alphabet letters that are not in the line c++

So, what I'm trying to do is, open and read a file, but read only the first line. I have already done this part. I have to read each character on that line and then I need to print the alphabet letters that are not found in that line.
Let's say the line was:
a b c D E f G H I j k L M n o
So, I will have to print the letters from p-z, because they are not in the line.
So, how will get the letters that are not in the line?!
This is what I have so far:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main(int argc, char *argv[]){
if(argc < 2){
cerr << "Usage: pangram <filename>" << endl;
return 0;
}
ifstream infile(argv[1]);
if (infile.good() == false){
cerr << "Error opening file[" << argv[1] << "]" << endl;
return 1;
}
char ch;
string line;
int a[26] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
int brk = 0;
while(infile.good()){
if(brk == 0) {
getline(infile, line);
for(int i=0; i <= line[i]; i++){
if(isalpha(line[i])) {
if(line[i] == )
cout << line[i] << endl;
}
}
}
brk ++;
if(brk == 1) {
break;
}
}
}
Here's a slightly modified version of your solution. I'm intentionally using a stack allocation of 26 bools instead of bitset so it's easier to comprehend.
bool letter_was_seen[26] = {}; // initialize an array to hold 26 bools (A..Z) all to false
getline(infile, line); // read a line into a string
size_t len = line.size();
for (size_t i = 0; i < len; i++)
{
char ch = ::toupper(line[i]); // convert to upper case
if ((line[i] >= 'A') && (line[i] <= 'Z')) // is it a valid letter A..Z ?
{
letter_was_seen[ch - 'A'] = true; // mark it's place in the array as "seen"
}
}
// now print the letters not seen within the line
for (char i = 'A'; i <= 'Z'; i++)
{
int index = (int)(i - 'A');
if (letter_was_seen[index] == false)
{
cout << i; // print the letter not seen
}
}
cout << endl; // print end of line to flush output
Another possible solution,
#include <deque>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
deque<char> allChars(26);
iota(allChars.begin(), allChars.end(), 'a');
char readString[] = "test xyz";
for (char& c : readString) {
allChars.erase(remove(allChars.begin(), allChars.end(), c),
allChars.end());
}
cout<<"Not removed: ";
for (char& c : allChars) {
cout<<c<<" ";
}
cout<<endl;
}

lower string characters and add a _ in front of converted capital letter

I have one more question, I want to add a _ in front of every Capital letter which will be converted to lowercase, plus the first letter cannot be capital!! I cant figure out how to do it... :{ example:
input: loLollL, output: lo_loll_l
and I want it to go backwards too: input: lo_loll_l output: loLollL
code is here:
#include <iostream>
#include <algorithm>
using namespace std;
int main ()
{
const int max = 100;
string slovo;
int pocet_r;
cout << "Zadaj pocet uloh:" << endl;
cin >> pocet_r;
if(pocet_r >= 1 && pocet_r <=100)
{
// funkcia na zabezpecenie minimalneho poctu chars
for (int i = 0; i <pocet_r; i++)
{
cout << "Uloha " << i+1 << ":" << endl;
cin >> slovo;
if(slovo.size() > max)
{
cout << "slovo musi mat minimalne 1 a maximalne 100 znakov" << endl;
}
while( slovo.size() > max)
{
cin >> slovo;
}
for (int i=0; i <= slovo.size(); i++)
{
int s = slovo[i];
while (s > 'A' && s <= 'Z')
{
if(s<='Z' && s>='A'){
return s-('Z'-'_z');
}else{
cout << "chyba";
}
}
}
cout << slovo[i] << endl;
}
}else{
cout << "Minimalne 1 a maximalne 100 uloh" << endl;
}
system("pause");
}
EDIT>
for (int i=0; i <= slovo.size(); i++)
{
while (slovo[i] >= 'A' && slovo[i] <= 'Z')
{
string s = transform(slovo[i]);
cout << s << endl;
s = untransform(s);
cout << s << endl;
}
}
This should work:
#include <string>
#include <cctype>
#include <iostream>
using namespace std;
string
transform(const string& s)
{
const size_t n = s.size();
string t;
for (size_t i = 0; i < n; ++i)
{
const char c = s[i];
if (isupper(c))
{
t.push_back('_');
}
t.push_back(tolower(c));
}
return t;
}
string
untransform(const string& s)
{
string t;
const size_t n = s.size();
size_t i = 0;
while (i < n)
{
char c = s[i++];
if (c != '_')
{
t.push_back(c);
continue;
}
c = s[i++];
t.push_back(toupper(c));
}
return t;
}
int
main()
{
string s = transform("loLollL");
cout << s << endl;
s = untransform(s);
cout << s << endl;
}