Program not reading Char String

ok i have to write a function that receives a pointer to a character string and a character. the function should return the number of times the charcter occurred in the string. ok my problem is If i declare a string i can get the program to work say for instance char strg1[]=" My car has a crack in it"; BUT i cant get it to work when i just use char strg1[50]; and if i cin>> enter in my car has a crack. it only reads my????????? y is this





#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cstring>


using namespace std;
int main()
{
int count(0);
int charcnt(char *ptr1, char c);
char strg1[50],strg2[]="c";

cout <<"Please enter in your first string up to 50 characters."<<endl<<endl;
cin >> strg1;
cout<< strg1;
char *ptr1(strg1);
while(*ptr1)
{
if(*ptr1 == 'c' )
count++;
ptr1++;
}
cout <<"The Char c appears "<<count<<" times in Strg1."<<endl;

system("pause");
return count;
}
closed account (S6k9GNh0)
1. I suggest you not use system() although that's not the problem here:
http://cplusplus.com/forum/articles/11153/

2. Here's an article explain how to use cin in a more correct manner:
http://cplusplus.com/forum/articles/6046/

I suggest you read up on both as they're decent reads.
Last edited on
Topic archived. No new replies allowed.