frustrated about functions

how many vowels in a user inputted sentence

Page 1 of 1

5 Replies - 637 Views - Last Post: 28 November 2007 - 01:29 PM Rate Topic: -----

#1 slippy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-November 07

frustrated about functions

Post icon  Posted 27 November 2007 - 02:29 PM

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

void start(char x[]); //function prototype
void fillit(char x[], int SIZE, int&, int&, int&, int&, int&);
const int SIZE = 80;

int main() {
  int num_a=0, num_e=0, num_i=0, num_o=0, num_u=0;
  char x[SIZE];

  start(x);
  fillit(x, SIZE);

  cout << "A occured " << num_a << " times" << endl;
  cout << "E occured " << num_e << " times" << endl;
  cout << "I occured " << num_i << " times" << endl;
  cout << "O occured " << num_o << " times" << endl;
  cout << "U occured " << num_u << " times" << endl;
}
void start(char x[SIZE]) {
  cout << "Input a sentence up to " << SIZE << " characters long, and I will te\
ll you how many times each vowel occurs in it." << endl;
  cin.getline(x, SIZE);
}

void fillit(char x[SIZE] int &num_a, int &num_e, int &num_i, int &num_o, int &n\
um_u) {
  int index = 0;
  int length = strlen(x);
  while (index < length){
	if(x[index] == 'a'){
	 num_a++;
	}
	if (x[index] == 'e'){
	  num_e++;
	}
	if (x[index] == 'i'){
	 num_i++;
	}
	if (x[index] == 'o'){
	 num_o++;
	}
	if (x[index] == 'u'){
	  num_u++;
	}
	index++;
  }
} 

now i'm really frustrated.
Ive been trying to finish an assignment all day and i havnt finished a single thing. I don't understand functions for the life of me, i get almost everything else but functions. WHY oh WHY won't this compile?! i keep getting errors no matter WHAT i do to my prototype OR the function itself. very disheartened, very frustrated :(

Is This A Good Question/Topic? 0
  • +

Replies To: frustrated about functions

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: frustrated about functions

Posted 27 November 2007 - 03:05 PM

Please post the errors.
Was This Post Helpful? 0
  • +
  • -

#3 foohoo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 10-August 07

Re: frustrated about functions

Posted 27 November 2007 - 03:06 PM

Try this:

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

void start(char x[]); //function prototype
void fillit(char x[], int&, int&, int&, int&, int&);
const int SIZE = 80;

int main() {
  int num_a=0, num_e=0, num_i=0, num_o=0, num_u=0;
  char x[SIZE];

  start(x);
  fillit(x, num_a, num_e, num_i, num_o, num_u);

  cout << "A occured " << num_a << " times" << endl;
  cout << "E occured " << num_e << " times" << endl;
  cout << "I occured " << num_i << " times" << endl;
  cout << "O occured " << num_o << " times" << endl;
  cout << "U occured " << num_u << " times" << endl;
  
  return 0;
}
void start(char x[SIZE]) {
  cout << "Input a sentence up to " << SIZE << " characters long, and I will tell you how many times each vowel occurs in it." << endl;
  cin.getline(x, SIZE);

}

void fillit(char x[SIZE], int &num_a, int &num_e, int &num_i, int &num_o, int &num_u) {
  int index = 0;
  int length = strlen(x);
  while (index < length){
	if(x[index] == 'a'){
	 num_a++;
	}
	if (x[index] == 'e'){
	  num_e++;
	}
	if (x[index] == 'i'){
	 num_i++;
	}
	if (x[index] == 'o'){
	 num_o++;
	}
	if (x[index] == 'u'){
	  num_u++;
	}
	index++;
  }
}




What i have done is put a
 return 0; 
in your main as it has to return an "int"
after that i removed the first "int" from your "fillit()" because you dont need to supply it with "SIZE" and after that it worked, you had done all the hard work! the first time i ran it i got the wrong output (wierd?!?) but everytime since it was ok!

hope this helps!

Foo
Was This Post Helpful? 0
  • +
  • -

#4 slippy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-November 07

Re: frustrated about functions

Posted 28 November 2007 - 09:19 AM

I did what you said foo, but got errors.

revised code:

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

void start(char x[]); //function prototype
void fillit(char x[], int&, int&, int&, int&, int&);
const int SIZE = 80;

int main() {
  int num_a=0, num_e=0, num_i=0, num_o=0, num_u=0;
  char x[SIZE];

  start(x);
  fillit(x, num_a, num_e, num_i num_o num_u);

  cout << "A occured " << num_a << " times" << endl;
  cout << "E occured " << num_e << " times" << endl;
  cout << "I occured " << num_i << " times" << endl;
  cout << "O occured " << num_o << " times" << endl;
  cout << "U occured " << num_u << " times" << endl;
  return 0;
}
void start(char x[SIZE]) {
  cout << "Input a sentence up to " << SIZE << " characters long, and I will te\
ll you how many times each vowel occurs in it." << endl;
  cin.getline(x, SIZE);
}

void fillit(char x[SIZE], int &num_a, int &num_e, int &num_i, int &num_o, int &\
num_u) {
  int index = 0;
  int length = strlen(x);
  while (index < length){
	if(x[index] == 'a'){
	 num_a++;
	}
	if (x[index] == 'e'){
	  num_e++;
	}
	if (x[index] == 'i'){
	 num_i++;
	}
	if (x[index] == 'o'){
	 num_o++;
	}
	if (x[index] == 'u'){
	  num_u++;
	}
	index++;
  }
}





these were the errors:
lab6.cxx: In function `int main()':
lab6.cxx:15: error: expected `)' before "num_o"
lab6.cxx:7: error: too few arguments to function `void fillit(char*, int&, int&,
 int&, int&, int&)'
lab6.cxx:15: error: at this point in file


This post has been edited by slippy: 28 November 2007 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#5 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: frustrated about functions

Posted 28 November 2007 - 09:34 AM

When you call the fillit() function you need to add a "," between the parameters you are passing in.
Was This Post Helpful? 0
  • +
  • -

#6 slippy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-November 07

Re: frustrated about functions

Posted 28 November 2007 - 01:29 PM

thank you very much, case closed, you guys are the best.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1