DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26

Thread: array..?

  1. #16
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    This is all you need...
    Code:
    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cctype>
    using namespace std;
    int 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 << (char)toupper(name[i]) << "\n";
    
    }
    return 0;
    
    }

  2. #17
    Join Date
    Aug 2007
    Posts
    24
    /* strupr example */

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;

    /* converts string to upper case characters */
    ptr = strupr(string);
    printf("%s\n", ptr);
    return 0;
    }
    i found dis example..i dont know how to initialize pointers..its a bit confusing..

  3. #18
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Why cant you use toupper()?

  4. #19
    Join Date
    Aug 2007
    Posts
    24
    i dont learnt yet how to use toupper() hehe .. and i'm using borland turbo c++..there is alot of error..

  5. #20
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    OK I had a quick look at strupr()..
    This will work only for 5 lettered names.. You will need to work out how to get around that.. Not too hard!

    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
    int i;
    
    char name[5];
    char* names;
    cout << "Please enter your name..(one word only).." <<endl;
    cin >> name;
    for (i = 0; i < 5; i++)
    	cout << name[i] << "\n";
    
    names = strupr(name);
    for (i = 0; i < 5; i++)
    	cout << names[i];
    cin >> i;
    return 0;
    
    }

  6. #21
    Join Date
    Aug 2007
    Posts
    24
    thanx code for the code..i modified it a bit a put #include <string.h> to make it zero error..maybe bcos i used the old version (borland turbo c++)
    anyway thanx dude..it works!!

    ps: thanx to others also who had helped me.. :)

  7. #22
    Join Date
    Nov 2003
    Posts
    4,118
    strupr is indeed nonstandard. You can use the toupper() function in a loop to convert all letters to uppercase.
    Danny Kalev

  8. #23
    Join Date
    Sep 2007
    Posts
    1
    Hi all........

    I am new to c++..
    I want to store string in an array.............
    So i tried it buy storing the string in 2D character array as given below...


    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    #include<stdio.h>
    //using std::string;
    class ReadyReeds{

    private:
    char choice[6];
    float price[6];
    char name[6][20];
    int i;

    public:
    ReadyReeds(){

    choice[0]='A';
    choice[1]='B';
    choice[2]='C';
    choice[3]='E';
    choice[4]='S';
    choice[5]='\0';

    price[0]=29.95f;
    price[1]=20.95f;
    price[2]=38.95f;
    price[3]=18.95f;
    price[4]=32.95f;

    name[0] = "AltoClarient";
    name[1] = "Bb Clarient";


    }

    int validate(char ch){
    return 0;

    }

    void display();

    };


    void ReadyReeds::display(){
    for(i=0; i<5; i++){
    cout<<"Choice = "<<choice[i]<<"\n";
    cout<<"price = " <<price[i]<<"\n";
    }// end of for
    }//end of function


    void main(){
    ReadyReeds rr;
    clrscr();
    rr.display();
    getch();

    }

    But the line that i have marked in red is raising error Lvalue required.
    I really appreciate if some on give a solution.........

  9. #24
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    I believe what you're trying to accomplish is best done during declaration.

    Code:
    char name[6][20] = {{"Alto Clarinet"}, {"Bb Clarinet"}};
    The braces are paramount.

    Also, I think you have to specify both subscripts when assigning values to 2-D arrays outside of an initialization(see above), i.e., this:

    Code:
     name[0] = "AltoClarient";
    name[1] = "Bb Clarient";
    ...won't work.

    Also, once you've gone past initialization I think you'll have to access each of those elements individually(with a nested loop or something similar), which, in your case seems tedious. Have you thought about using string? I see that you have commented out a using directive for string, which tells me you've though about it.

    I think(emphasis on think) that you can get away with that for output, but not assignments.

    Here is some sample code:

    Code:
    char name[6][20] = {{"Alto Clarinet"}, {"Bb Clarinet"}};
    
    cout<< name[0] << '\n' << name[1] << endl;
    Output:
    Alto Clarinet
    Bb Clarinet

  10. #25
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    You got some working codes, but you probably wanted something simple like this:#include <iostream.h>
    #include <string.h>
    void main()
    {
    int i;
    char name[10]; //Made it a 1D array
    cout << "Please enter your name..(one word only).." <<endl;
    //Take in the name directly
    cin >> name;
    for(i=0; name[i] != '\0'; ++i)
    cout << name[i] << endl;

    }



    ==== Your original code (with comments showing the mistakes)...===
    #include <iostream.h>
    #include <string.h>
    void main()
    {
    int i;
    char name[10][10]; //Unnecessary 2D array
    cout << "Please enter your name..(one word only).." <<endl;
    for (i=0; i>=10; i++); //Semi column after for loop makes it a useless code
    {
    cin >> name[i]; //This will read in one string as a whole into name[i]
    }
    cout << name[i]; //Print the whole of name[i], exactly what you entered

    } //The mistake here is you have made place for 10 strings, and are using only one.
    //What you should have done is made place for one string, an array of 10 chars, inputted the string, and printed each character in a loop
    //like the above code (the one I've written) does.

  11. #26
    Join Date
    Aug 2007
    Posts
    24
    thanx for the reply..now i can understand better in array.. :)

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