cin.ignore?
Aug 28, 2015 at 2:45am UTC
i have to write a program that reads a sentence/1 character and will display all the characters selected in uppercase/lowercase.
problem i have is the sentence i enter gets lots into garbage or something/im not sure what happens there. and my loop counter for the characters not sure to use the ascii characters or the letters.
thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include<iostream>
#include <conio.h>
using namespace std;
void characterCount( char [100] , char );
//---------------------------------------------------------------------------
int main()
{
char sentence[100];
char character;
cout<< "enter a sentence " ;
cin.get(sentence[100]);
cin.ignore(256, '\n' );
cout<<"enter a character " ;
cin>> character;
cout<<"your sentence was " << sentence[100]; //this part only displays first leter from sentence.
cout<<"\n\nyour character was " << character << endl;
characterCount(sentence,character);
getch();
return 0;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void characterCount( char words[100], char choice )
{
int upper = 0;
int lower = 0;
for ( int i = 0;i < '\0' ; i++)
{
// 'A' 'Z'
if ( choice < '65' && choice > '90' )
{
while (words[i])
{
choice = words[i];
putchar (toupper(choice));
i++;
}
upper + 1;
upper++;
cout << "you have selected " << choice << "times" << upper;
}
// 'a' 'z'
if ( choice < 'a' && choice > 'z' )
{
lower + 1;
lower++;
cout << "you have selected " << choice << "times" << lower;
}
cout<< " " << i <<" " ;
}
cout << "uppercase character " << choice << " "
<< upper << " times" <<endl;
cout << "lowercase character " << choice << " "
<< lower << " times" << endl;
Aug 28, 2015 at 3:36am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include<iostream>
// #include <conio.h>
// using namespace std;
#include <cctype>
// void characterCount( char[100] , char );
const int SZ = 100 ;
void characterCount( const char [SZ] , char );
//---------------------------------------------------------------------------
int main()
{
//char sentence[100];
char sentence[SZ];
char character;
std::cout << "enter a sentence " ;
// cin.get(sentence[100]);
std::cin.get( sentence, SZ ) ;
std::cin.ignore(256, '\n' );
std::cout << "enter a character " ;
std::cin >> character;
// cout<<"your sentence was "<< sentence[100]; //this part only displays first leter from sentence.
std::cout << "\n\nyour sentence was '" << sentence << "'\n\n"
<< "your character was '" << character << "'\n\n" ;
characterCount(sentence,character);
// getch();
std::cout << "\nenter any character to quit: " ;
std::cin >> character ;
// return 0; // implicit
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void characterCount( const char words[SZ], char choice )
{
if ( std::isalpha(choice) )
{
const char lower_case_choice = std::tolower(choice) ;
const char upper_case_choice = std::toupper(choice) ;
int upper = 0;
int lower = 0;
for ( int i = 0; i < SZ ; ++i )
{
if ( words[i] == lower_case_choice ) ++lower ;
else if ( words[i] == upper_case_choice ) ++upper ;
}
std::cout << "upper case character '" << upper_case_choice << "' " << upper << " times\n"
<< "lower case character '" << lower_case_choice << "' " << lower << " times\n"
<< " in all " << upper + lower << " times\n" ;
}
else std::cout << "character '" << choice << "' is not alphabetic\n" ;
}
Topic archived. No new replies allowed.