new to programing need help!!

This is a discussion on new to programing need help!! within the C++ Programming forums, part of the General Programming Boards category; Hellif o, This is probably a simple fix for the experienced programer, I am trying to have the user input ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    6

    Unhappy new to programing need help!!

    Hellif o,

    This is probably a simple fix for the experienced programer,

    I am trying to have the user input three letters of his choice and then out put those letters in reverse order.

    This is what I have so far:

    Code:
    //A program that prints out the letters in reverse of the users input
    
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	char ABC;
    	char reverse;
    	char letters;
    	char CBA;
    
    	//tells the user what is going to happen
    	cout<<"This program will reverse the three letters you input\n";
    
    	cout<<"Please enter three letters====>ABC\n";
    	cin>>letters;
    
    	reverse=CBA;
    
    	cout<<"Here are the letters in reverse<<CBA\n";
    
    	
    
    	return 0;
    }
    As you can see I am a real beginner.

    Thank in advance
    Last edited by Dave_Sinkula; 09-09-2006 at 02:49 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You need a for loop most likely: http://www.cprogramming.com/tutorial/lesson3.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    why dont you use C++ string instead of C strings?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    6

    hey

    Like I said. I am just starting out in the programming world and I need help with this for a school assignmnet.

    I am that new that I am reading my first text book in programming with C++

    NG

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Since letters is a char variable, it can only hold one character; you probably want to read in three characters.
    Code:
    int main(void) {
        char a, b, c;
    
        cin >> a >> b >> c;
        cout << c << b << a;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    An alternative would be to use getch() (unstandard), from conio.h. there are lots of examples onthe boards, search for it if you're interested. This doesn't require the user to press return whenever they enter a key. However, there are bad things about it, mainly it being non-standard.

    do not be worried! There is an alternative! Arrays of characters, or std::string 's.

    Code:
    char Letters[3];
    holds info for a number of letters, instead of the char letter; that you would have had (notice the added s).

    Code:
    cin >> Letters; // Careful not to overflow
    for ( int i=strlen( Letters ); i>0; i-- ) cout<< Letters[i];
    prints it in reverse. I prefer std::string though!

    Code:
    #include <iostream>
    #include <string>
    
    int main( void )
    {
      std::string MyStdString;
      
      std::cout<< "Enter a string: ";
      std::cin >> MyStdString;
    
      for ( int i=0; i<MyStdString.size(); i++ ) std::cout<< MyStdString[i]; // Right order
      
      std::cout<< '\n';
    
      for ( int i=MyStdString.end(); i>=myStdString.begin(); i-- ) std::cout<< MyStdString[i]; // reverse order
    
      return 0;
    }
    Last edited by twomers; 09-09-2006 at 08:21 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You forgot the std:: on the rest of your code, like cout and cin.

    My code seems simpler, if only 3 characters are required.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Thanks, dwks. I've only recently come to enjoy the looking of std::'s on front of things, and am, unfortunately, in the habit of using namespace std;'s and forget :/

    I agree, dwks, yours is simpler, but alternatives. Perhaps the OP doesn't know about arrays. This may give him/her something to look into. Afterall they're "new to programming and need help"

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You missed one
    Code:
    cout<< '\n';
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    Thanks all,

    A lot of what you posted made me get into the books.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    I have another question?

    I keep getting a C1004 error.

    I can not figure what I am missing,

    as said before I am new to progarming.



    //This program will caculate the income tax due on the amount inputed by the user

    #include <iostream>
    using namespace std;


    int main()

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);


    double anual_income,tax_due;
    int a(0%),b(26%),c(29%);
    const int d(3380);

    {
    cout<<"Please hit enter after every entry\n";
    cout<<"Please enter you anual income for the year $\n";
    cin>>anual_income;

    if (anual_income<=12000);
    tax_due=anual_income*a;

    else if ((anual_income>12000) && (anual_income<=25000));
    tax_due=(anual_income-12000)*b;

    else if (anual_income>25000);
    tax due=((anual_income-25000)*c)+d;

    cout<<"Anual Income = $" <<anual_income<<endl
    <<"Tax Due = $" <<tax_due <<endl;


    return 0;

    }

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Please post the full text of the error and point out the line that it refers to.Also please use [code][/code] tags when posting code.

    In the code above, you have semicolons after your if expressions (e.g. "if (anual_income<=12000);"). Remove those. You also have some brace issues. Finally, you can't use % in theinitialization of variables, what were you trying to do there?

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    USE CODE TAGS
    Just had to make sure he noticed...

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    Okay okay, I get the picture.

    I am trying to show the user what his taxes will be based on the inputed anual income.

    These are the two errors I am getting.

    I made the corrections that was indicated.

    error C2146: syntax error : missing ';' before identifier 'cout'

    fatal error C1004: unexpected end of file found

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    I hope this is better, As I said I made the corrections.



    Code:
    //This program will caculate the income tax due on the amount inputed by the user
    
    #include <iostream>
    using namespace std;
    
    
    int main()
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    
    double anual_income,tax_due;
    int a(0),b(0.26),c(0.29);
    const int d(3380);
    
    {
    	cout<<"Please hit enter after every entry\n";
    	cout<<"Please enter you anual income for the year $\n";
    	cin>>anual_income;
    
    	if (anual_income<=12000)
    	tax_due=a;
    
    	else if ((anual_income>12000) && (anual_income<=25000))
    	tax_due=(anual_income-12000)*b;
    
    	else if (anual_income>25000)
    	tax due=((anual_income-25000)*c)+d;
    
        cout<<"Anual Income = $" <<anual_income<<endl
    		<<"Tax Due = $" <<tax_due <<endl;
    
    
    	return 0;
    
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing for DNA...
    By S16 in forum C Programming
    Replies: 9
    Last Post: 04-29-2009, 09:25 AM
  2. Programing microcontrollers in c
    By gorabhat in forum C Programming
    Replies: 2
    Last Post: 09-09-2008, 10:40 AM
  3. Human brain and programing
    By avgprogamerjoe in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-27-2007, 04:48 PM
  4. Non programing Question
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 06-21-2002, 06:55 AM
  5. C++ Programing
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-06-2001, 04:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21