DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: array..?

  1. #1
    Join Date
    Aug 2007
    Posts
    24

    array..string..?

    #include <iostream.h>
    #include <string.h>
    void main()
    {
    int i;
    char name[10][10];
    cout << "Please enter your name..(one word only).." <<endl;
    for (i=0; i>=10; i++);
    {
    cin >> name[i];
    }
    cout << name[i];

    }



    //i get confused with dis..if i enter name as 'flame'isnt it suppose to be like dis..
    f
    l
    a
    m
    e

    but the output is 'flame'..how i can get it to be as above..? :confused:
    thanx if you give me any idea or advice or help me.. ;)
    Last edited by inflame254; 09-07-2007 at 11:41 PM.

  2. #2
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    Code:
    cout<< name[i] <<endl;

  3. #3
    Join Date
    Aug 2007
    Posts
    24
    it doesnt work..the output is still

    Please enter your name..(one word only)..
    james << entered by user..
    james

    how to make the output like dis..?
    j
    a
    m
    e
    s

    anyway thanx for ur advice dude~ :)
    Last edited by inflame254; 09-07-2007 at 09:47 PM.

  4. #4
    Join Date
    May 2007
    Posts
    843
    cout << name[i] << "\n";

    I hope this help.

  5. #5
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    Hmm, works for me.

    If 'endl' isn't working, I don't see why '\n' would. I mean, they're more or less the same, right, output buffer flushing notwithstanding?

  6. #6
    Join Date
    Aug 2007
    Posts
    24
    #include <iostream.h>

    void main()
    {
    int i;
    char name[10][10];
    cout << "Please enter your name..(one word only).." <<endl;
    for (i=0; i>=10; i++);
    {
    cin >> name[i];
    }
    cout << name[i] << "\n";

    }

    i replace the code as above..n still the output is not vertical..it just skip 1 line of the name entered by the user..

  7. #7
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    I just noticed that you're using a 2-D array. Why?

    Remove the second '[10]' from your declaration of variable name[] and see what happens...

  8. #8
    Join Date
    Aug 2007
    Posts
    24
    #include <iostream.h>

    void main()
    {
    int i;
    char name[10];
    cout << "Please enter your name..(one word only).." <<endl;
    for (i=0; i>=10; i++);
    {
    cin >> name[i];
    }
    cout << name[i] << "\n";


    }

    like dis..?

    if i entered 'name'
    the output is only a letter 'n'..
    it still dont show 'name' in vertical..like
    n
    a
    m
    e



    i just learnt dis c++ programing :( sory for the noob question..

  9. #9
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    This:

    Code:
    for (i=0; i>=10; i++);
    ...won't work either.

    1) You need to remove the semi-colon.

    2) the relation should look like this:
    Code:
    for (i = 0; i <= 10; i++)
    With 'i >= 10' the loop will never run because i is not greater(>) than 10 to begin with.

  10. #10
    Join Date
    Aug 2007
    Posts
    24
    finally it works!!! thanx dude..
    one more.. how to use strupr to make the output is in capital letter..?

  11. #11
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    I'm not familiar with that particular function.

    If you're just wanting to uppercase one character at a time look into the function toupper.

  12. #12
    Join Date
    Aug 2007
    Posts
    24
    i want to make all the characters uppercase...i had done dis but it contain lot of error..

    #include <iostream.h>
    #include <string.h>
    #include <stdio.h>
    void main()
    {
    int i;

    char name[10];
    cout << "Please enter your name..(one word only).." <<endl;
    for (i = 0; i <= 10; i++)

    {
    cin >> name[i];
    cout << name[i] << "\n";

    }

    char *name2 = name[i];
    name2=strupr(name[i]);
    printf("%s\n", name[i]);

    }

  13. #13
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    I'm not sure strupr is in the STL. You should find a function that is. If, in fact, it is not in the STL it(your code) may not be 'widely' portable.

    What do your errors say? I can see what's wrong with this block of code, the strupr stuff notwithstanding:

    Code:
    char *name2 = name[i];     //make one change and this assignment becomes valid.
    name2=strupr(name[i]);    //I'm ignorant of 'strupr'. Sorry.
    printf("%s\n", name[i]);      //make the same change as above and this will become valid.
    Having said that, the first two lines are nullified by the last as you never end up using name2.

    What do you think is wrong with it?

  14. #14
    Join Date
    Aug 2007
    Posts
    24
    hurm..actually im using borland turbo c++..and i dont know what is STL..is it also a programing software..?
    the errors when i compiled dis in borland..:
    1: cannot convert 'int' to 'char*' in function main()
    2: type mismatch in parameter '__s' in call to 'strupr(char *)' in function main()
    3: 'name2' is assigned a value that is never used..

  15. #15
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    When I said STL I should have said 'C Standard Library' which is a collection of headers and libraries. A vastly important collection.

    Those last three lines of code are generating the errors. Check up on how you initialize pointers.

    Get rid of that strupr function. I've found very little quality documentation on it and what I have found says it's not standardized.

Similar Threads

  1. Replies: 27
    Last Post: 02-11-2016, 06:29 AM
  2. Reversing an Array
    By Tmcclain in forum Java
    Replies: 7
    Last Post: 02-13-2009, 11:57 PM
  3. redim help !!!
    By angelito in forum VB Classic
    Replies: 1
    Last Post: 11-21-2005, 07:16 AM
  4. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 10:05 AM
  5. Replies: 2
    Last Post: 04-15-2005, 09:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center