C++ function overloading cannot identify char - c++

When I input two integers, the output is correctly their difference. However when I enter a string and a char, instead of returning how many times the char appears in the string, it returns -1, which is the out put for error. Could anyone please help me? It's just my second day learing c++...
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
void mycount(int a, int b)
{
std::cout<< a - b <<std::endl;
}
void mycount(char str[], char s[])
{
int len,i;
int sum=0;
len = strlen(str);
for (i=0;i<len;i++){
if (strncmp(&str[i],&s[0],1) == 0){
sum = sum + 1;
};
};
printf("results: %d times\n",sum);
}
int main()
{
int a,b;
char c[200],d;
if(std::cin>> a >> b){
mycount(a,b);
}
if(std::cin>> c[200] >> d){
mycount(a,b);
}
else{
std::cout<< "-1" <<std::endl;
}
std::cin.clear();
std::cin.sync();
}

Hint - what will this program print?
#include <iostream>
using namespace std;
int main()
{
char c[200],d;
cout << sizeof(c) << endl;
cout << sizeof(d) << endl;
return 0;
}
Answer:
200
1
That declaration does not do what you think it does - c is an array of 200 chars, d is a single char. It's a feature of the C declaration syntax, same as:
int *c, d;
c is a pointer to int, d is an int.
Since you are doing C++, why not make your life easier and use std::string instead?

A few changes should fix your problems. First when inputting an array with cin use getline and call ignore right before hand. I find it easier to pass s as a char instead of an array of size one make sure your call your second my count with c and d instead of a and b.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
void mycount(int a, int b)
{
std::cout<< a - b <<std::endl;
}
void mycount(char str[], char s)
{
int len,i;
int sum=0;
len = strlen(str);
for (i=0;i<len;i++){
if (strncmp(&str[i],&s,1) == 0){
sum = sum + 1;
};
};
printf("results: %d times\n",sum);
}
int main()
{
int a,b;
char c[200],d;
if(std::cin>> a >> b){
mycount(a,b);
}
std::cin.ignore();
if(std::cin.getline (c,200) && std::cin >> d){
mycount(c,d);
}
else{
std::cout<< "-1" <<std::endl;
}
std::cin.clear();
std::cin.sync();
}
These changes should fix it.

Related

Deleting characters in a char array

#include <iostream>
#include <string.h>
#include <algorithm>
# define N 100
using namespace std;
int main()
{
char A[N];
unsigned char APP[256] = {0};
cout << "Insert string" << endl;
cin.getline(A,100);
for(int i=0; i < strlen(A); ++i)
{
unsigned char B = A[i];
if(!APP[B])
{
++APP[B];
cout << B;
}
}
return 0;
}
/*char eliminazione(char,char)
{
}*/`
I have to put the for in the "delete" function calling the value B and print it in main, do you know how to do it?
Given a string A read from the keyboard, create a function in C ++ language that calculates a second string B obtained from the first by deleting all the characters that appear more than once. The resulting string must therefore contain the characters of the first string, in the same order, but without repetitions.
Instead of 'cout', you just store the characters into a new string, and increment its index, see code below as an example:
#include <iostream>
#include <string.h>
#include <algorithm>
# define N 100
using namespace std;
int main()
{
unsigned char A[N]; // better to have same type for both A and B
unsigned char B[N];
memset(B, 0, sizeof(B));
unsigned char APP[256] = {0};
cout << "Insert string" << endl;
cin.getline(A,100);
int j = 0;
for(int i=0; i < strlen(A); ++i)
{
unsigned char c = A[i];
if(!APP[c]++)
B[j++] = c; //cout << c;
}
cout << B << endl;
return 0;
}
Output:
Insert string
lalalgdgdfsgwwyrtytr
lagdfswyrt

How can I take char value with int?

How can I take char value with int value?
For example I want enter + because I'm making a calculator program:
#include <iostream>
int main() {
int a;
std::cin >> a;
std::cout << a;
return 0;
}
you can see the value of int into char by static casting your int to char.
std::cout << static_cast<char>(a);

istringstream-function string_to_int cannot take c-str, why?

I learned a helper function that can convert strings to integers:
int string_to_int(string s)
{
istringstream instr(s);
int n;
instr>>n;
return n;
}
It's mentioned that the argument s cannot be c-str string, why is this the case?
But you can pass a C style string.
The reason for that is because the std::string constructor can implicitly accept a CharT* (Char type, which is char in this case) as a parameter. Thus, something like the following would work:
#include <iostream>
#include <sstream>
using namespace std;
int string_to_int(string s)
{
istringstream instr(s);
int n;
instr>>n;
return n;
}
int main()
{
const char* test = "12345";
std::cout << string_to_int(test) << "\n"; // Outputs 12345
std::cout << string_to_int("122") << "\n"; // Outputs 122
}

Can't put class with main function?

Something I am working on is making a code that focuses on making a class that reverses an order of numbers. This will then get put into the main code that will eliminate any trailing zeroes. I can't seem to wrap my head around how classes work and where I am going wrong. Here is my code:
Numbers.h
#pragma once
#include <iostream>
class Numbers
{
public:
int digit
private:
void Numbers::reverse();
};
Numbers.cpp
#include "Numbers.h
#include <iostream>
using namespace std;
void Numbers::reverse(){
int n, reversedNumber = 0, remainder;
cout << "Enter the number you would like to manipulate! " << endl;
cin >> n;
while (n !=0)
{
remainder = n % 10;
reversedNumber = reversed Number * 10 + remainder;
n /= 10;
}
//return *this;
}
Main.cpp
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include "Numbers.h"
using namespace std;
int main()
{
Numbers.reverse;
system("pause");
return 0;
}
I can't seem to make the reverse function in my Numbers.cpp work in the main function. I am new to C++ and am not sure where I am going wrong. Any help would be appreciated!
OK, there are a lot of mistakes or learning errors in your code. Your header file should look something like:
#pragma once
class Numbers
{
public:
Numbers();
~Numbers();
int Reverse(int input); // Function is 'public'.
};
Your CPP file will then be (parts taken from S.O. post here):
#include "Numbers.h"
Numbers::Numbers()
{
}
Numbers::~Numbers()
{
}
// No need to store the value in 'digit' since this
// is just an algorithm which can return the result.
int Numbers::Reverse(int input)
{
int ret = 0;
while(input > 0)
{
ret = ret * 10 + (input % 10);
input = input / 10;
}
return ret; // Return the reversed number and let the user decide what to do.
}
Then you can use your class as follows:
#include "Numbers.h"
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number to reverse: ";
cin >> num;
Numbers numClass;
cout << "Reversed number is: " << numClass.Reverse(num) << endl;
return 0;
}

Convert char to const char* String Insert C++

I have a problem with String Insertion because, I can not add a char, just a const char. How can i easily convert it?
The compiler just accept like this:
b.insert(i,"a");
But i want like this:
b.insert(i,b[ii]);
Full Code:
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string a,b;
int aa=0;
cin >> a;
b=a;
for(int i=0;i<a.length()+1;i++)
{
for(int ii=0;ii<a.length();ii++)
{
b.insert(i,a[ii]);
if (b == string(b.rbegin(), b.rend()))
{
cout << b << endl;aa=1;
break;
}
b.erase (b.begin()+i);
}
if(aa=1)break;
}
if(aa==0)
cout << "NA" << endl;
return 0;
}
See the documentation for std::string::insert. The version that takes a single char also needs a count argument.
b.insert(i,1,b[ii]);
You should use the following overload of std::string::insert:
basic_string& insert( size_type index, size_type count, CharT ch );
See http://en.cppreference.com/w/cpp/string/basic_string/insert