Please help with this program.

Hello.

I have to write a program that registers information about a n number of people, categorize them depending on how much they donated and finally outputs all the info to a txt file.

I found someone else doing something similar on this forum and im trying to analyze that code to do what i need but the objetive is not the same and im lost.

Right now the problem is that the program skips some of the necessary user inputs and then enters in a infinite loop.

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
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{

	int contributors, money;
	string name;
	string phone;
	cout << "How many contributors? ";
	cin >> contributors;

	int counter = 0;
	int counter1 = 0;
	int counter2 = 0;

	while (counter < contributors)
	{
		cout << "\nContributor #" << counter + 1 << "\nEnter name: ";
		getline(cin, name);

		cout << "\nEnter amount of contribution: ";
		cin >> money;

		if (money < 500) 
		{
			cout << "Please enter a donation between 500 and 20000.";
		}
		else  if (money > 20000)
		{
			cout << "Please enter a donation between 500 and 20000.";
		}
		else
		{
			cout << "Please enter phone number(10 digits): "; //Should only accept a string of 10 numbers
			getline(cin, phone);


		}


	}


		return 0;
}
Last edited on
Since you didn't increment the counter variable you had an infite loop.
while (counter < contributors)
I quickly hacked together a little skeleton, maybe you want/can complete it.
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
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

struct Contributer
{
  string name;
  string phone;
  int amount;
};

const int MIN_AMOUNT = 500;
const int MAX_AMOUNT = 20000;

int get_num_contributers ();
void get_contributers (const int count, vector<Contributer> &contributers);
void save_contributers (string filename, vector<Contributer> &contributers);
bool valid_phonenumber (string phone);


int main (void)
{
  try
  {
    int num_contributers = get_num_contributers ();
    vector<Contributer> contributers (num_contributers);
    get_contributers (num_contributers, contributers);
    // maybe some output or processing needs to be done here
    save_contributers ("contributers", contributers);
  }
  catch (exception &ex)
  {
    cerr << "ERROR - " << ex.what ();
    exit (EXIT_FAILURE);
  }
  system ("pause");
  exit (EXIT_SUCCESS);
}

int get_num_contributers ()
{
  int count;
  cout << "How many contributors? ";
  cin >> count;
  
  return count;
}

void get_contributers (const int count, vector<Contributer> &contributers)
{
  for (int i = 0; i < count; i++)
  {
    Contributer con = {"N/A", "N/A", 0};
    cout << "Name: ";
    cin >> con.name;
    do
    {
      cout << "\nAmount between " << MIN_AMOUNT << " and " << MAX_AMOUNT << ": ";
      cin >> con.amount;
    } while (con.amount < MIN_AMOUNT && con.amount > MAX_AMOUNT);

    do
    {
      cout << "\nPhone (max 10 digits) : ";
      cin >> con.phone;
    } while (!valid_phonenumber (con.phone));    
  }
}
void save_contributers (string filename, vector<Contributer> &contributers)
{
  //TODO - place your code here
}

bool valid_phonenumber (string phone)
{
  //TODO - place your code here
  return true;
}
Thanks! This really helps me
I have a couple questions, i hope you can help/clarify me:

Im trying to do the part that verifies that the phone entered is a 10 digit number:
1
2
3
4
5
6
7
8
9
10
11
bool valid_phonenumber(string phone)
{
	if (phone.length() != 10)
		
		return false;
	for (auto x : phone) {
		if (x >'9' || x <'0')
			
			return false;
	}
	return true;


That code, as is, works. It keeps asking for the correct input until it is entered.
However, if i add a cout << "Please enter a 10 digit number; before each return false it will enter in a loop and ask for a number indefinitely.
Why is this?

[This part is now solved] All needed to do was chnging the and operator(&&) for an or(||) one.
And also... the part that verifies that the amount donated does not seem to work
while (con.amount < MIN_AMOUNT && con.amount > MAX_AMOUNT);
For some reason the && operator is not working there and is passing everything as true. It will work if I only leave one of the sided. Why is this happening?

Last edited on
And also... the part that verifies that the amount donated does not seem to work
while (con.amount < MIN_AMOUNT && con.amount > MAX_AMOUNT);
For some reason the && operator is not working there and is passing everything as true. It will work if I only leave one of the sided. Why is this happening?


Silly mistake of me - it must be while (con.amount < MIN_AMOUNT || con.amount > MAX_AMOUNT);
Topic archived. No new replies allowed.