Bumpkins of Bumpus

We're just getting more into functions and this is one of our programs that we have to do. It's apparently a familiar one with many program students. I keep trying to compile the code (I use visual studio 2012, if that's of any use to mention) but it will only give me "Press any key to continue..." and that will be the only line I will see in the command window.

I'm guessing the issue is that it wants an input file but just isn't getting one.

SO... here are my questions
1)How do I do input files and the like in visual studio
2)Is there anything about this code that I need to fix

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


//function prototypes
string calcDoor(float b, float d);
float conv2cm(float value, char unit);

int main()	
{

	int populate;	//	Bumpkins to check for
	ifstream census;
	census.open("bumpkin.in");
	census >> populate;

	for (int i = 0; i < populate; i++)	
	{
		string name;		// Bumpkin's name
		int doorways;		// Doorways bumpkin will enter
		float b;		// Bumpkin's height
		float d;		// Doorway's height
		char unit;		// Unit of measurement

		// Name, doorways, height, unit and doorway heights
		census >> name >> doorways >> b >> unit;
		cout << "Bumpkin #" << i + 1 << ": " << name << " ";
		cout << "entering " << doorways << " doors.\n";
		cout << name << " has a height of " << b << unit << endl << endl;

		if (unit != 'c')	
		{
			cout << name << "\'s height is converted to " << conv2cm(b, unit) << "c" << endl << endl;	
		}


		for (int j = 0; j < doorways; j++)	
		{

			cout << name << " in doorway #" << j + 1 << ": ";
			census >> d >> unit;
			cout << calcDoor(b, conv2cm(d, unit));
		}

	}
	
	
system("pause");
return 0;

}


string calcDoor(float b, float d)	
{
	if (d > b * 1.25)			return "Stilts\n";
	if (b * 1.25 >= d && d > b * 1.05)	return "Walk\n";
	if (b * 1.05 >= d && d > b * 0.65)	return "Duck\n";
	if (b * 0.65 >= d && d > b * 0.40)	return "Crawl\n";
	if (b * 0.40 >= d && d > b * 0.25)	return "Limbo. Good luck!\n";
	if (b * 0.25 >= d)					return "Blocked\n";	
}

float conv2cm (float value, char unit)	
{
	//	Convert values to centimeters for main computing
	//	Why can't we still go metric yet? Oh right. Bumpkins.
	if (unit == 'i')	return value * 2.54;
	else if (unit == 'f')	return value * 30.48;
	else if (unit == 'y')	return value * 91.44;
	else if (unit == 'm')	return value * 100;
	else if (unit == 'c')	return value;	
}




> I'm guessing the issue is that it wants an input file but just isn't getting one.

1. Give the complete path to the input file.
2. In the program, check that you have been able to open the file.

For instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    // this is just an example of a complete path
    // replace with the actual path to your input file
    const std::string path_to_file =
                "C:/Documents and Settings/karpl/UserData/bumpkin/bumpkin.in" ;

    std::ifstream file(path_to_file) ;

    if(file) std::cout << "opened the file successfully\n" ;

    else
    {
        std::cerr << "error in opening file: " << path_to_file << '\n' ;
        return 1 ;
    }

    // rest of main
}
I'm a bit confused on what the 'std::' part of this code is. Up to where we are in our class we haven't gone over what that is yet.

After looking more into the Bumpkin problem it apparently requires a lot of things that we haven't really gone over during our class :/
Karpl wrote:
I'm a bit confused on what the 'std::' part of this code is. Up to where we are in our class we haven't gone over what that is yet.
So you haven't done the hello world program yet?
1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << "Hello, World!" << std::endl;
}
Most likely your teacher accidentally taught you to use using namespace std; and forgot to teach you why it was bad: http://stackoverflow.com/q/1452721
Last edited on
Most likely your teacher accidentally taught you to use using namespace std; and forgot to teach you why it was bad: http://stackoverflow.com/q/1452721


Most likely he did (either that or the book we're having to use) We're expected to use using namespace std; and nothing else as far as I know. He also never taught us how to use input files in visual studio 2012. It's an online class, and he posted the wrong video about it.
Last edited on
> He also never taught us how to use input files in visual studio 2012

Read these tutorials:
http://www.mochima.com/tutorials/fileIO.html
http://www.cplusplus.com/doc/tutorial/files/
Topic archived. No new replies allowed.