Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,420 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,986 people online right now. Registration is fast and FREE... Join Now!




y not working?

 
Reply to this topicStart new topic

y not working?, counting ASCII from 32(space) to 126(tilde).

issacc
19 Jan, 2008 - 06:31 AM
Post #1

New D.I.C Head
*

Joined: 11 Jan, 2008
Posts: 3

CODE

#include <iostream>

using namespace::std;

void main(){
    char ch;
    

    int 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,zero,one,two,three, four , five,six , seven, eight, nine;
    while ( cin.get(ch) )
        if ( ch == 'a'|| 'A' )     ++a;
        if ( ch == 'b'|| 'B' )     ++b;
        if ( ch == 'c'|| 'C' )     ++c;
        if ( ch == 'd'|| 'D' )     ++d;
        if ( ch == 'e'|| 'E' )     ++e;
        if ( ch == 'f'|| 'F' )     ++f;
        if ( ch == 'g'|| 'G' )     ++g;
        if ( ch == 'h'|| 'H' )     ++h;
                if ( ch == 'i'|| 'I' )     ++i;
        if ( ch == 'j'|| 'J' )     ++j;
        if ( ch == 'k'|| 'K' )     ++k;
        if ( ch == 'l'|| 'L' )     ++l;
        if ( ch == 'm'|| 'M' )     ++m;
        if ( ch == 'n'|| 'N' )     ++n;
        if ( ch == 'o'|| 'O' )     ++o;
        if ( ch == 'p'|| 'P' )     ++p;
        if ( ch == 'q'|| 'Q' )     ++q;
        if ( ch == 'r'|| 'R' )     ++r;
        if ( ch == 's'|| 'S' )     ++s;
        if ( ch == 't'|| 'T' )     ++t;
        if ( ch == 'u'|| 'U' )     ++u;
        if ( ch == 'v'|| 'V' )     ++v;
        if ( ch == 'w'|| 'W' )     ++w;
        if ( ch == 'x'|| 'X' )     ++x;
        if ( ch == 'y'|| 'Y' )     ++y;
        if ( ch == 'z'|| 'Z' )     ++z;
        if ( ch == '0')     ++zero;
        if ( ch == '1')     ++one;
        if ( ch == '2')     ++two;
        if ( ch == '3')     ++three;
        if ( ch == '4')     ++four;
        if ( ch == '5')     ++five;
        if ( ch == '6')     ++six;
        if ( ch == '7')     ++seven;
        if ( ch == '8')     ++eight;
        if ( ch == '9')     ++nine;
    
        
        cout<<a<<"a's were found. \n";
}

1.what i trying to do is to calculate elements from 32 to 126 in ASCII. i need to record down how frequent each element type by user. i know this not the right way to write it, but this is first idea since i look the question....

2.Y my "cout<<a<<"a's were found. \n";" not working?But when i put "{}" within the "while" and "if", then the answer come out but repeating the answer.

3.After done the coding above, i was thinking can we directly compare the input with the ASCII?  


User is offlineProfile CardPM
+Quote Post

Max_Payne
RE: Y Not Working?
19 Jan, 2008 - 08:06 AM
Post #2

D.I.C Head
**

Joined: 19 Oct, 2007
Posts: 51


My Contributions
Hope this helps:

CODE

#include <iostream>
using namespace std;

int main () // i suggest you start using int main
            // ..I use int main, as it is always good
            // ..for your programs to return some useful value.
{
    char ch; // no need to initialize it because you will
             // .. enter a value to it with cin >> ch;
    
    int a = 0; // variables that are not initialized should be initialized

    cout << "Enter a number or a character (# to end) \n> ";
    cin >> ch;

    // while ch is not equal to '#' do the following
    while (ch != '#')
    {
        cout << "> ";
    
        if ( ch == 'a'|| ch == 'A' )     ++a;

        cin >> ch; // now that you are inside the loop you need another cin >> ch
    }
        
    cout << endl << a << " a's were found. \n\n"; // print the number of a's

    system ("PAUSE"); // freeze console screen to see values,
                      // ..not C++ standard but for example purposes
                      // ..it does the job

    return 0; // you need this if you use int main
              // it indicates that the program ended successfully
}


This post has been edited by Max_Payne: 19 Jan, 2008 - 12:00 PM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Y Not Working?
19 Jan, 2008 - 11:57 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
This if ( ch == 'a'|| 'A' ) ++a; is probably not what you meant. Try this instead if ( ch == 'a'|| ch == 'A' ) ++a;,

I'd strongly suggest using an array. Also, consider looking at the ascii values of the characters are. Using numbers could make this much easier.

Hope this helps.
User is offlineProfile CardPM
+Quote Post

issacc
RE: Y Not Working?
21 Jan, 2008 - 03:52 AM
Post #4

New D.I.C Head
*

Joined: 11 Jan, 2008
Posts: 3

QUOTE(baavgai @ 19 Jan, 2008 - 12:57 PM) *

This if ( ch == 'a'|| 'A' ) ++a; is probably not what you meant. Try this instead if ( ch == 'a'|| ch == 'A' ) ++a;,

I'd strongly suggest using an array. Also, consider looking at the ascii values of the characters are. Using numbers could make this much easier.

Hope this helps.



thx for ur suggestions baavgai. now im using array now. hope done my home work soon...

QUOTE(baavgai @ 19 Jan, 2008 - 12:57 PM) *

This if ( ch == 'a'|| 'A' ) ++a; is probably not what you meant. Try this instead if ( ch == 'a'|| ch == 'A' ) ++a;,

I'd strongly suggest using an array. Also, consider looking at the ascii values of the characters are. Using numbers could make this much easier.

Hope this helps.



thx for ur suggestions baavgai. now im using array now. hope done my home work soon...
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:16AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month