Char Array collecting position of an indivudal chracter.

This is a discussion on Char Array collecting position of an indivudal chracter. within the C++ Programming forums, part of the General Programming Boards category; Hi, im trying to work out how to obtain the position in an array that an indivudal character(or number) is ...

  1. #1
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69

    Question Char Array collecting position of an indivudal chracter.

    Hi,

    im trying to work out how to obtain the position in an array that an indivudal character(or number) is located.

    For example:


    Code:
    char formula[100]; //this is array I want to search
    int counter=0;
    int x = 0;
    	
    	cout<<"enter linear forumla: ";
    	cin>>formula;
    	cout<<"forumla entered: "<<formula<<"\n\n";
    
    //Testing for x variables
    
    	for(counter=0;counter<sizeof(formula);counter++)
    		{
    		if(formula[counter] == 'x' )
    		x++;
    
    		}
    
     cout<<"\n"<<x;
    Ive managed to count how many 'x''s there are in the array but I want to be able to work out the postition in the array they are (ie 0, 5, 12, 556! etc...)

    I hope you understand what I mean

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    for(int i=0; i<sizeof(formula); i++)
    {
      if(formula[i] == 'x')
      {
        std::cout << "x found at index " << i << std::endl;
      }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    thanks for your answer

    but when I try to compile I get this error:

    type qualifier 'std' must be a struct or class name.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Did you include:

    #include <iostream>

    Note: Not iostream.h
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    ok, I jsut tried including #<iostream>, and it said it couldnt find the file, when i did #<iostream.h> it was ok with it but still got the same error about 'std'

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    <iostream> is the new standard header, <iostream.h> is an old one. If you have an old compiler, simply remove the std:: notation, ie: cout instead of std::cout.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Just use the cout as you did in the rest of the program. Magos didn't read your code carefully enough and used a different form of the objects.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    if u want to know the number of an individual character then
    Code:
       if(formula[count]=='x')
            x++;
            cout <<x;
    and if u want the place of 'x' in the array then
    Code:
        if(formula[count]=='x')
            cout << count ;

  9. #9
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    Hi all


    it all works now thnx, Ive just realised how simple it was as well... I was being really silly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  2. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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