3 Replies - 329 Views - Last Post: 15 May 2011 - 08:43 PM Rate Topic: -----

#1 Bane.C++  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 13
  • Joined: 14-May 11

I want to convert this program into a struct, But How?

Posted 15 May 2011 - 05:59 PM

here's a code I made for Counting the numbre of occurences for a string, I want to convert this into a Struct so I can enter both numbers and letters, I also wanted to make this program as simple as possible, with using the library #define, <iomanip> so I can just declare a simple constant for the size, here's the code.


#include <iostream>
#include <conio>
#include <cstring>
#include <iomanip>
#include <ctype>
#define SIZE 26

void count(char[]);

void main ()
{

	char sentence [1000];

   	cout<<"enteer your string" <<endl;
		cin.getline (sentence, 1000);
      count(sentence);


cin.get();
cin.get();

}

void count(char s[])
{
  char alphabet[]= {'a' , 'b',  'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
  						'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

	int i, count[SIZE]= {0};

   	for (i=0 ; i<strlen(s) ; i++)
      	{
         s[i]=tolower(s[i]);
         	for (int j= 0 ; j<SIZE; j++)
            	if (s[i] == alphabet [j])
               		count [j]++;

   }

   	for (i= 0 ; i<SIZE; i++)
      	if (count [i]>0)

         	cout<<count[i]<<" "<<alphabet[i]<<endl;

  }                                               








and here's the struct code I tried to convert.

Struct array
{
 double numbers (int a);
 void count_sentence (char s[]);



};


void main ()
{
  const int max_size = 1000;

	char sentence [1000];

   	cout<<"enter your string" <<endl;
		cin.getline (sentence, 1000);
                count_sentence(sentence);


cin.get();
cin.get();

}

void count_sentence(char s[])
{
  char alphabet[]= {'a' , 'b',  'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
  						'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

	int i, count_sentence[max_size]= {0};

   	for (i=0 ; i<strlen(s) ; i++)
      	{
         s[i]=tolower(s[i]);
         	for (int j= 0 ; j<max_size; j++)
            	if (s[i] == alphabet [j])
               		count_sentence [j]++;

   }

   	for (i= 0 ; i<max_size; i++)
      	if (count [i]>0)

         	cout<<count_sentence[i]<<" "<<alphabet[i]<<endl;

  }                                               

void count_numbers (int a)
{
    for (i=0 ; i<max_size ; i++);
          { 
            loop_number[i];
     
	int i, count_numbers[max_size]= {0};

   	for (i=0 ; i<strlen(a) ; i++)
      	{
         a[i]=(a[i]);
         	for (int j= 0 ; j<max_size; j++)
            	if (a[i] == loop_numbers [j])
               		count [j]++;

   }

   	for (i= 0 ; i<max_size; i++)
      	if (count_numbers [i]>0)

         	cout<<count_numbers[i]<<" "<<loop_number[i]<<endl;

}






there so many errors, I'm new to C++ i've made some changes but still this is all I can do, can someone help me? the code above is mine, with a help from our instructor (for some improvements for the first code, just a little bit).

Is This A Good Question/Topic? 0
  • +

Replies To: I want to convert this program into a struct, But How?

#2 Astraeus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 205
  • Joined: 17-November 09

Re: I want to convert this program into a struct, But How?

Posted 15 May 2011 - 06:30 PM

What errors are you getting? Please post them.

The first thing I see that is noticeable is that your struct is capitalized Struct array

C is case sensitive, therefore Struct and struct (the correct data type name) are not synonymous.

Also, you have to make an object for of your structure such as array a; When calling functions and using variables from your structure, you must use the object when calling functions.

a.function();

Read more on structures here.

This post has been edited by Astraeus: 15 May 2011 - 06:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 Bane.C++  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 13
  • Joined: 14-May 11

Re: I want to convert this program into a struct, But How?

Posted 15 May 2011 - 06:52 PM

There's so many errors (the usual error's I think?)

have you tried running it?

yes, maybe I must use that but how can I make a substitute for the given define size?

should I declare it int the struct?

the case sensitiveness is not the problem (because it's easy to check).


My problem is I can't run my code, maybe I lack of something that I'm not sure of.. :(
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: I want to convert this program into a struct, But How?

Posted 15 May 2011 - 08:43 PM

If you are getting errors post the entire error messages exactly as they appear in your development environment. While these error messages may not mean much to you yet the have valuable information to aid in locating the problems.

I will guess that some of the first errors are about not finding include files.

#include <conio>
#include <cstring>
#include <iomanip>
#include <ctype>



In the above snippet you are trying to include a file named conio, There is no standard C++ header named conio. Also <ctype> should be <cctype>

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1