Help with this C++ Program

This is a discussion on Help with this C++ Program within the C++ Programming forums, part of the General Programming Boards category; Write a program that declares a character array named alphabet and initializes the array to "ABCDEFGHIJKLMNOPQRSTUVWXYZ" In your program, include ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    16

    Help with this C++ Program

    Write a program that declares a character array named alphabet and initializes the array to "ABCDEFGHIJKLMNOPQRSTUVWXYZ" In your program, include a FOR LOOP that replaces one character of the array at a time with the lowercase letters a-z. Print the character array to the screen during each iteration of the loop. Hint: Remember that a character array is an array of integer values. Use ASCII values to make the changes.

    Okay, I got it to print out everything at once. How would I make it so you press a key, and it does it each time.

    Because right now, its showing everything at once.

    http://img292.imageshack.us/img292/9...ubitmapyz1.jpg

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <string.h>
    #include <math.h>
     
    int main()
    {
    char alphabet[27] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    
    for (int i=0;i<=25;i++)
    {alphabet[i] = alphabet[i] + 32;
    	cout << alphabet << "\n";
    }
    system("pause");
    return 0;
    }
    Last edited by JDK721; 12-11-2006 at 02:07 PM.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Any key? If just the enter key, use cin.get().
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Not the enter key, something else.. enter key is used to close the program.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There is no standard way to make the code wait for a keypress without the user having to hit the enter key. You could use a function specific to your platform (OS and compiler), like maybe getch(). However, it would probably be better to just use the enter key and keep your code portable.

    BTW, <iostream.h> and <iomanip.h> are old and non-standard. They don't work on some modern compilers. You should be using <iostream> and <iomanip> (although you don't need <iomanip> in this program). If your instructor/book/tutorial is teaching you to use these, you should know that they are quite a bit out of date.

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Also <cmath> not <math.h>. Plus, why have string.h?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Also <cmath> not <math.h>. Plus, why have string.h?
    Both <math.h> and <string.h> are standard and will work on modern compilers, although it is of course preferable to use <cmath> and <cstring> if you need them. Of course, in this program, neither of those are necessary either.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Daved
    >> Also <cmath> not <math.h>. Plus, why have string.h?
    Both <math.h> and <string.h> are standard and will work on modern compilers, although it is of course preferable to use <cmath> and <cstring> if you need them. Of course, in this program, neither of those are necessary either.
    Is there any functional difference between <math.h> and <cmath>, or <string.h> and <string>, in a C++ program? And is there an ongoing trend towards doing away with all of the .h header files in C++, or is it expected that some of them will remain permanently?

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    <string.h> (properly <cstring>) and <string> are completely different things.

    Yes, C++ has done away with the ".h" in all standard libraries. The old C libraries (e.g "math.h") are prefixed "c" -> <cmath>
    One difference is that everything is in std:: namespace, helping to avoid naming collisions (which is a good thing).

    (Recently I tried to include both <windows.h> and <allegro.h> in a program, but I couldn't do it naively, because of a naming collision: both define a struct BITMAP to begin with.)

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I said cmath as a matter of personal preference, to make it clear. Also, the OP should take note that both <math.h>/<cmath> and <string.h>/<cstring>/<string> are not needed in this program.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by anon
    <string.h> (properly <cstring>) and <string> are completely different things.

    Yes, C++ has done away with the ".h" in all standard libraries. The old C libraries (e.g "math.h") are prefixed "c" -> <cmath>
    One difference is that everything is in std:: namespace, helping to avoid naming collisions (which is a good thing).
    One program I wrote used the relatively new <inttypes.h> header, which apparently has only a C and not a C++ version yet (<cinttypes> doesn't exist). So far that's the only such case I've seen, though.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    That's probably because <inttypes.h> is C99, and not standard in C++.

    All C standard library headers that were standard in C when the C++ standard was written are also standard in C++. The C versions with the '.h' are deprecated, and the preferred alternatives for C++ programs are the versions without the '.h' and with the 'c' prefix. You can use either version in a C++ program and it will still compile on a standards-conforming compiler.

    Headers added to C99 (after the C++ standard) are not standard in C++, but probably work on some compilers that also support C.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    We use microsoft visual C++ 5.0 at school, which I believe is 1998 so thats why you see old headers.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    That's too bad. The Express version of the latest Visual C++ is free, and there are other free and modern options available as well. You have to work with what they require, just remember that it is outdated if you continue C++ programming beyond your current school.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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