Student info

Hey everyone!
I need some help with this code. I need to create a program that acts as a student information library, where it stores the students' first name, last name, and grade. We are also supposed to be able to view their profiles.
The code is a bit long (and unfinished in the "view student info" section), but please help!

Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cstdlib>
#include <string>
#define NAMESIZE 20

struct Record 
	{
		char firstName[NAMESIZE];
		char lastName[NAMESIZE];
		int	score;
	};

using namespace std;

bool get_valid_word(const string& prompt, string& word, bool (*is_valid)(const string&)=0);
bool get_valid_int(const string& prompt, int& n, bool (*is_valid)(int)=0);

int main()
{
	
	const string prompt_firstName="Please enter the first name: ";
	const string lastName="Please enter the last name: ";
	const string score="Please enter the score: ";
}
	
bool get_valid_word//(const string& prompt, string& word, bool (*is_valid)(const string&)=0) 
{
	string line;
	while(1){
			cout<<prompt<<endl;
			if (getline(cin,line))
			{
				istringstream iss(line); // is equal to: istringstream iss=line; assigns the value stored in line to iss'
				if (iss>>word
						&& is_valid(word))
						{
							return true;
						}
			}
			else{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(),'\n');
			return false;
			}
	}
	return true;
}

bool validWord (const string& word) //valid: bob. invalid: bob is awesome asdfh12345 only grab first word of the line 
{
	if (word.size()>20)
	{
	return false;
	}
	for (string::size_type i=0; i<word.size();i++) //size_type only for string. int for string.
	{
		if (!isalpha(word[i]))
			return false;
	}
	return true;
}
bool validScore (int score)
{
	if (score>100||score<0)
	{
		return false;
	}	//google what is function pointer?
	return true;
}


The problem I receive from the compiler is:

line 26: error: 'bool get_valid_word' redeclared as different kind of symbol
line 15: error: previous declaration of 'bool get_valid_word(const std::string&, std::string&, bool (*)(const std::string&))
line 26: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
line 28: error: expected primary-expression before 'line'
line 28: error: expected '}' before 'line'
line 29: error: expected unqualified-id before 'while'

I know this program has a lot of errors, but if you could work out only one or two, I'd be very grateful!
Last edited on
Please ignore some of the commented sections. They were notes to myself. :)
you need to include <sstream>

line 26 you need to uncomment the parameter list

I'm not sure what this is supposed to be bool (*is_valid)(const string&)=0 but it causes a compiler error for me.
Last edited on
bool (*is_valid)(const string&)=0It is a function pointer with default value provided.
I would use typedefs or using like:
1
2
3
using validatorPtr = bool (*)(const string&);
//or
typedef bool (*validatorPtr)(const string&);

And use resulting type like:
bool get_valid_word(const string& prompt, string& word, validatorPtr is_valid = nullptr)
Last edited on
At line 26 you have missed parentheses. Note: when you put these parentheses, you will find many errors in get_valid_word go and solve them.
Thank you
Topic archived. No new replies allowed.