Thread: What am I missing?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    What am I missing?

    (Again a reminder that this language is still new to me )

    I'm working on two programs, the first counts the number of vowels in a sentance inputed by the user, and uses a value returning function isVowel but I keep getting these two errors..

    error C2065: 'ch' : undeclared identifier
    error C2448: '<Unknown>' : function-style initializer appears to be a function definition
    Am I missing something simple that I just can't see?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int isVowel();
    
    int main()
    {
    
    	char ch;
    
    	cout << "Enter a sentence: ";
    	cin.get(ch);
    	return ch;
    }
    
    int isVowel(cin.get(ch))
    {
    	char v;
    	int count;
    	count = 0, v = 0;
    
    	while (ch != '\n')
    	{
    		if(ch == 'A' || ch == 'a' || ch =='E' || ch == 'e'
    			|| ch == 'I' || ch == 'i' || ch == 'O' || ch == 'o'
    			|| ch == 'U' || ch == 'u')
    			count++;
    		v++;
    	}
    
    	cout << "There are" << v << "in this sentance" << endl;
    	return 0;
    }
    My second is supposed to determine an estimated population after a number of years from the current population, birth rate, death rate, and number of years inputed by the user using a growthRate funtion(birth, death), and an estimatedPopulation funtion(current pop, growth rate, and n) I managed to get past the 24 errors (lol I said I was new to this) but I still have 4 warnings about variables being used without being initialized, how do I correct this?

    Code:
    #include<iostream>
    using namespace std;
    
    int growthRate (int, int);
    int estimatedPopulation (int, double, int);
    
    int main()
    {
    	int pop;
    	int birth;
    	int death;
    	int num;
    	
    	cout << "Enter current population: ";
    	cin >> pop;
    	
    	cout << "Enter birth rate: ";
    	cin >> birth;
    
    	cout << "Enter death rate: ";
    	cin >> death;
    	
    	cout << "Enter the number of years for the projected population: ";
    	cin >> num;
    
    
    	return 0;
    }
    
    int growthRate(int birth, int death)
    {
    	int pop;
    	int popgrowth;
    
    	if (pop > 2)
    		cout << "Population must be greater than 2 ";
    	
    
    	if (birth > 0)
    		cout << "Birth rate must be a positive number";
    
    
    	if (death > 0)
    		cout << "Death rate must be a positive number";
    
    	popgrowth = birth - death;
    	
    	cout << "Growth rate = " << popgrowth << "%" << endl;
    	return popgrowth;
    }
    
    int estimatedPopulation (int currentpop, int popgrowth, int num)
    {
    	int pop;
    	int estpop;
    	int birth;
    	int death;
    
    	currentpop = pop + (birth * pop / 100) - (death * pop / 100);
    
    	estpop = currentpop * num;
    
    	cout << "Projected population after" << num << "years = " << estpop << endl;
    	return estpop;
    }

  2. #2
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    #include <iostream>
    
    using namespace std;
    
    void isVowel();
    
    int main()
    {
    
        char ch;
    
        cout << "Enter a sentence: ";
        cin.get(ch);  // Does this function work OK with getting chars?
        isVowel(ch);
        //return ch;
    
        return 0; // Always return 0 in main
    }
    
    void isVowel(char ch)
    {
        char v;
        int count;
        count = 0, v = 0;
    
        while (ch != '\n')
        {
            if(ch == 'A' || ch == 'a' || ch =='E' || ch == 'e'
                || ch == 'I' || ch == 'i' || ch == 'O' || ch == 'o'
                || ch == 'U' || ch == 'u')
                count++;
            v++;
        }
    
        cout << "There are" << v << "in this sentance" << endl;
        //return 0;
    }

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    cin.get(ch);
    You should use the getline function to get the entire sentence as opposed to reading in an individual character. (you could also put cin.get(ch) into a loop, read each character individually and test for vowelness)
    Last edited by The Brain; 01-27-2005 at 10:49 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Here are some helper comments on the population C++ source:
    Code:
    #include<iostream>
    using namespace std;
    
    int growthRate (int, int);
    int estimatedPopulation (int, double, int);
    
    int main ()
    {
      int pop;
      int birth;
      int death;
      int num;
    
      cout << "Enter current population: ";
      cin >> pop;
    
      cout << "Enter birth rate: ";
      cin >> birth;
    
      cout << "Enter death rate: ";
      cin >> death;
    
      cout << "Enter the number of years for the projected population: ";
      cin >> num;
    
    
      return 0;
    }
    
    int growthRate (int birth, int death)
    {
      int pop;
      int popgrowth;
    
      // pop is uninitialized
      /*if (pop < 2) // Changed the sign > to <
      {
         cout << "Population must be greater than 2 ";
         return -1;  // Added this to exit function
      } */
    
      if (birth < 0)    // Changed the sign > to <
      {
        cout << "Birth rate must be a positive number";
        return -1;      // Added this to exit function
      }
    
      if (death < 0)    // Changed the sign > to <
      {
        cout << "Death rate must be a positive number";
        return -1;      // Added this to exit function
      }
    
    
      popgrowth = birth - death;
    
      cout << "Growth rate = " << popgrowth << "%" << endl;
      return popgrowth;
    }
    
    int estimatedPopulation (int currentpop, int popgrowth, int num)
    {
      int pop;
      int estpop;
      int birth;
      int death;
    
      // pop is uninitialized
      //currentpop = pop + (birth * pop / 100) - (death * pop / 100);
    
      // currentpop is uninitialized
      // estpop = currentpop * num;
    
      // estpop is uninitialized
      // cout << "Projected population after" << num << "years = " << estpop << endl;
      // return estpop;
    
      // We have to return something now that I've commented everything lol
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In addition to comments already made I offer the following.

    Your error messages have to do with the definition of isVowel(). The first error is because the ch appearing in the definition argument list is undeclared. The second error is because the definition needs to indicate the type of any argument, if there is going to be one.

    In general, the definition and declaration of the function need to agree as to return type, function name, number/type/order of arguments. In your case, isVowel isn't needed, unless required by some external, arbitrary force, such as a teacher who wants you to practice writing functions.

    v is declared to be of type char. Within isVowel you initialize v with the value 0 and then increment v with the ++ operator within the while loop. While it is possible to initialize variables of type char with an int I doubt that is what you had in mind. Also, while it is possible to increment variables of type char with the ++ operator, I doubt it is what you had in mind, either. Also note that your output statement has nothing to do with how many vowels are in the sentence. When you get things straightened out, v will be the number of char in the sentence. v isn't needed at all, as far as I can see, if your goal is to count the number of vowels.
    Last edited by elad; 01-27-2005 at 01:14 PM.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM