Thread: 3 small char problems

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    Red face 3 small char problems

    hi guys I am having a bit of trouble with chars my problem is as follows:

    c. (No prompts or explanatory text necessary.) The user types a 3-digit number which you input as 3 separate chars. You then compute the number that represents and output it as a single value. A sample run of your program might go as
    540
    540
    The first line represents 3 char inputs that you do, and the second line represents one unsigned (or int) output that you do.

    d. Same as c, except this time you do a single input of an unsigned (or int) and three char outputs. An example run of your program might go as
    540
    540

    e. (Still no prompts or explanatory text necessary.) The input (you may assume without checking) is 2 chars: an upper case letter followed by a lower case letter. The output is the letters in reverse order, but the first letter output is to be uppercase and the second letter output is to be upper case. A sample run of your program might go as
    Au
    Ua


    This is my code so far note my professor wanted us to do all 9 programs in 1 .cpp file thus the obnoxious large code.





    Code:
    //  CS 575 HW 3 
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    void a();
    void b();
    void c();
    void d();
    void e();
    void f();
    void g();
    void h();
    
    
    bool die( const string & msg );
    
    
    int main(){
    
    
    /*
        unsigned n;
        cout <<"Gimme a # in [13,19]: ";
        cin >>n;
        if( !cin )  die( "C'mon, man, that's not even a number!" );
        if( n < 13 )  die( "too small" );
        if( n > 19 )  die( "too large" );
    
    
        cout <<"Thanks for the " <<n <<endl;
        cout <<"Thanks for using this amazing program" <<endl;
    */
        a();
        b();
        c();
        d();
        e();
        f();
        g();
        h();
    
    
    }
    
    
    void a(){
        unsigned dollars, quarters, dimes, nickels, pennies;
        cin >>dollars >>quarters >>dimes >>nickels >>pennies;
        cout <<100*dollars+25*quarters+10*dimes+5*nickels+pennies <<endl;
    }    //  a
    
    
    void b(){
        unsigned n;
        cin >>n;
        if( !cin )  die( "non-numeric input" );
        if( n < 10000 ) die( "too small" );
        if( n > 99999 ) die( "too large" );
        cout <<n/100 <<endl;
    }    //  b
    
    
    void c(){
        char n[3];
        cin >>n;
        cout<< n << endl;
    }    //  c
    
    
    void d(){
    }    //  d
    
    
    void e(){
    }    //  e
    
    
    void f(){
        string phoneNo;
        cin >>phoneNo;
        if( phoneNo.size() == 13                   &&
            phoneNo[0] == '('                      &&
            '2' <= phoneNo[1] && phoneNo[1] <= '9' &&
            '0' <= phoneNo[2] && phoneNo[2] <= '9' &&
            phoneNo[8] == '-' )
            cout <<"ok" <<endl;
        else
            cout <<"bogus" <<endl;
    }    //  f
    
    
    void g(){
        unsigned c0, c1;
        cin >>c0 >>c1;
        if( !cin )  die( "non-numeric input" );
        if( c0 < 1 || c0 > 13 || c1 < 1 || c1 > 13 )  die( "out of range" );
        if( c0 == c1 )
            cout <<"pair" <<endl;
        else
            cout <<"nothing" <<endl;
    }    //  g
    
    
    void h(){
    }    //  h
    
    
    
    
    bool die( const string & msg ){
        cerr <<endl <<"Fatal error: " <<msg <<endl;
        exit( EXIT_FAILURE );
    }   //  d
    in problem in c I am stuck the compiler sends me an error e #2 stack around the variable "n" was corrupted. do i need if and elste statements? I was thinking of char to int conversion but its too confusing as I am still amateur.

    Please understand I am still beginner thus your wisdom shall save me from my igorance. I will keep updating as I find solutions to my homework maybe it helps others as well.

    Thanks for your time.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What are you inputting for the variables? Also I would comment out any of the functions that you have working and just work on one function at a time. When you use a C-string do not forget that you need to leave space for the end of string character ('\0') so if you need to input 3 characters into a C-string then the size of that C-string must be at least 4.

    Also you should be using more descriptive variable and function names.

    Jim

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by elpedoloco View Post
    in problem in c I am stuck the compiler sends me an error e #2 stack around the variable "n" was corrupted. do i need if and elste statements? I was thinking of char to int conversion but its too confusing as I am still amateur.
    Problem 1: You cannot use cin to input into a char array.
    Problem 2: You did not read your homework assignment. It clearly says to input them as 3 separate characters. Assuming you did not learn about std::getline yet, this tells me you should simple have 3 char declarations and input that way.
    Problem 3: Are you suppose to validate your input until you get a good entry? Or just quit like you have been doing?

    Quote Originally Posted by elpedoloco View Post
    Please understand I am still beginner thus your wisdom shall save me from my igorance. I will keep updating as I find solutions to my homework maybe it helps others as well.

    Thanks for your time.
    You should take a look through the first couple of our C++ Made Easy Tutorials. That may help you with this assignment.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Problem 1: You cannot use cin to input into a char array.
    You can use cin to input values into a char array.

    However I do agree that the assignment does seem to prohibit the use of C-strings.

    Jim

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by jimblumberg View Post
    You can use cin to input values into a char array.

    However I do agree that the assignment does seem to prohibit the use of C-strings.

    Jim
    Ok, my friend, for the pedantic yes you can; however as noted above it is a bad idea due to lack of bounds checking. The equivalent would be using gets() in C. Yes it works, but should you?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AndrewHunter
    Ok, my friend, for the pedantic yes you can; however as noted above it is a bad idea due to lack of bounds checking.
    Not really, e.g.,
    Code:
    char str[10];
    std::cin >> std::setw(10) >> str;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by laserlight View Post
    Not really, e.g.,
    Code:
    char str[10];
    std::cin >> std::setw(10) >> str;
    And for the extremely pedantic, Hello Laser , yes. However I doubt our friend the OP would know of / allowed to use such constructs. Additionally, things such as this only serve to obscure the problem at hand.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Quote Originally Posted by AndrewHunter View Post
    And for the extremely pedantic, Hello Laser , yes. However I doubt our friend the OP would know of / allowed to use such constructs. Additionally, things such as this only serve to obscure the problem at hand.
    hello thanks for the insight in my issue guys I've succesfully done part c which took me a few since I am a bit slow :P
    but ether way I just want to make sure I am doing it right. here is my code which compiles successfully.

    Code:
    //  CS 575 HW 3 
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    
    void c();
    void d();
    void e();
    void f();
    void g();
    void h();
    
    
    bool die( const string & msg );
    
    
    int main(){
    
    
    
    
        c();
        d();
        e();
        f();
        g();
        h();
    
    
    }
    
    
    
    
    
    
    void c(){
    	char digits[4];
    	cin.getline(digits, 4);
    	cout << digits << endl; // why when I input digits[3] it will                             //only output 2?
    		
    }    //  c
    
    
    void d(){
    }    //  d
    
    
    void e(){
    }    //  e
    
    
    void f(){
      
    }    //  f
    
    
    void g(){
      
    }    //  g
    
    
    void h(){
    }    //  h
    
    
    
    
    bool die( const string & msg ){
        cerr <<endl <<"Fatal error: " <<msg <<endl;
        exit( EXIT_FAILURE );
    }   //  die
    I had a few questions regarding my assigment, the first line represents 3 char imputs so 3 numbers but valued as chars?
    second line represents one unsigned or (int) output that you do.
    so I need a line on my code that has unsigned int? or is my output seen as an int, unsigned? kind of confusing, thanks for help !

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, your assignment says 3 char inputs. I think you are over complicating things here. Something like:
    Code:
    void chartodecimal(){
         char a,b,c;
         unsigned int number;
         cin>>a>>b>>c;
         ..do your conversion...
         cout << number;
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Thanks Andrew that made more sense, however in the conversion output I am getting weird numbers, I know this is due to the ascii code table which has 255 characters according to Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion.
    I am trying my output to be the same as the input. Whenever i do

    123 my output becomes as 49,
    Code:
    void c(){
    	 char x,y,z;
         unsigned int digit;
         cin>>x>>y>>z;
    	 digit = (x),(y),(z); // 123 in output is 49? 
         cout << digit;

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    That is because you are suppose to convert those characters into 1 unsigned integer, per your assignment. That is not how you convert them. This is a basic conversion, if I gave you two characters, '5' and '0' in that order, how would you convert them to the number 50? e.g. 5 times what? + 0 times what?

    Think about how the decimal number system works. The number 256 is 2 what, 5 what, and 6 what?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Quote Originally Posted by AndrewHunter View Post
    That is because you are suppose to convert those characters into 1 unsigned integer, per your assignment. That is not how you convert them. This is a basic conversion, if I gave you two characters, '5' and '0' in that order, how would you convert them to the number 50? e.g. 5 times what? + 0 times what?

    Think about how the decimal number system works. The number 256 is 2 what, 5 what, and 6 what?
    well i keep thinkin arithmetically, you'd multiply 5x10 and 0x10 as well? to give you 50? My teacher keeps telling us that compiler does not use understand basic arithmetic rules
    so I might be wrong the answer I just gave I do see the over all idea nonetheless if you could give me an example I'd grasp from there,

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, let's do an example. I give you the number 1035. To break this up we would go by the decimal place values:
    1 x 1000 = 1000
    0 x 100 = 0
    3 x 10 = 30
    5 x 1 = 5

    Then we would add all those up: 1000 + 0 + 30 + 5 = 1035. Do you see?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    // CS 575 HW 3
    Odd...CS 575 implies that you are not an amateur or a beginner, as stated in your first post. Does your school do something wacky, like start you off in 500-level courses, with 100-levels being the final year courses? Just a non-standard nomenclature, AFAIK. Heck, most 500-level courses are grad school courses.

    (Just an aside...oddities fascinate me)
    Last edited by rags_to_riches; 09-23-2011 at 01:54 PM.

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Awesome, so for my problem I understand that:
    x takes value of 100s
    y takes value of 10s and
    z takes value of 1s

    I tried to do the conversion as follows
    digit =(x+100)+(y+10)+(z+1) however my silly attempt did not work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One small doubt with char pointer
    By dipesh.nomad in forum C Programming
    Replies: 6
    Last Post: 07-23-2011, 09:18 PM
  2. small problems .. need help ..
    By Kawaii JoJo in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2009, 03:00 AM
  3. Problems, small program- HELP!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 03-01-2002, 09:08 AM
  4. 2 small problems
    By Robert_Ingleby in forum Windows Programming
    Replies: 3
    Last Post: 11-21-2001, 06:16 AM