Arrays/Strings

Hey everyone. I'm attempting to write a code that will display the distance between two rooms. I have most of the program correct, I just can't figure out how to display the names of the rooms by their actual names (ie. Room C123.7) rather than with just a number. I have JUST begun learning about arrays, and strings, so it is confusing me. My work so far is shown below. Any help or tips are appreciated! I'm new to C++, and have been teaching myself, so I am sure I am making quite a few mistakes. Thank you!

1
2
3
4
5
6
7
8
#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;

int main ()

1
2
{
    double length_in_feet = 0;
// Variable for length in feet. Globally initialized

1
2
{
    double length_in_meters = 0; 
// Variable for length in meters locally initialized
1
2
 double sum = 0;
    const double constant = 3.2808; 
// Constant for meters to feet conversion locally initialzed
char indicator = 'y'; // Loop as long as 'yes' is entered

while(indicator == 'y') // Loop as long as 'yes' is entered
1
2
3
4
{
      cout << "Enter measured length in meters"
             << endl;
        cin >> length_in_meters;
// Enter measured length of wire
1
2
3
4
 sum += length_in_meters;
        cout << endl
             << "Is there another cable length for you to enter? (enter y to enter another)? ";
        cin >> indicator; 
// Read the indicator

1
2
3
 }
    
    length_in_feet = sum * constant;
// Conversion from meters to feet
cout << "Your length is " << length_in_feet << " in feet" // Output in feet
1
2
3
4
5
6
7
8
9
<< endl << endl;
    }
    
    {
        const char MAX = 100;
        char location_1 [MAX];
        
        cout << "Enter the room and rack where the cable begins.";
        cin.getline(location_1, MAX);
// Location where cable begins

1
2
3
4
5
6
7
8
9
 }
        
    {
        
        const char MAX = 100;
        char location_2 [MAX];
        
        cout << "Enter the room and rack where the cable ends.";
        cin.getline(location_2, MAX, '\n');  
// Location where cable is terminated
1
2
3
4
5
6
7
8
}
        
    {
        const char MAX = 100;
        char cable_type [MAX];
        
        cout << "Enter the type of cable being used.";
        cin.getline(cable_type, MAX, '\n');
// Type of cable being used
1
2
3
4
5
6
7
8
9
10
11
12
13
14
}
        
    {
        const char MAX = 100;
        char location_1 [MAX];
        char location_2 [MAX];
        char cable_type [MAX];
        
        cout << "The distance between " << location_1 << " and " << location_2 << " is " << length_in_feet << " using " << cable_type << " cable.";
    }
    
    return 0;
    
    }

Last edited on
Reformat with code tags <> please.
Apologies, I just did
No you haven't. You need to highlight your code and then click the <> button to the right of the text box, then submit. you code will be enclosed like this
1
2
3
4
code
code
code
code
Last edited on
1
2
3
4
5
6
7
8
{
 const char MAX = 100;
 char location_1 [MAX];
 char location_2 [MAX];
 char cable_type [MAX];

 cout << "The distance between " << location_1 << " and " << location_2 << " is " << length_in_feet << " using " << cable_type << " cable.";
 }


Is this the block you are talking about? location_1, _2, and cable_type contain junk values, so you won't get anything meaningful when you display them.

I also notice that you have a lot of blocks like the one above. This limits the scope of your variables.
1
2
3
4
5
6
7
8
{
   int var;
}

{
//Despite the same name, this and the previous variable are two, completely different variables.
   int var; 
}


Limiting scope is not a bad thing, but make sure you use your variables for as long as you need them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main(){
    using namespace std;

    const unsigned max_size = 100;
//There is this handy-dandy class called std::string from <string> you can use instead.
    char user_input[max_size] = {'\0'};

    cout << "Please enter a phrase under 100 characters in length: ";
    cin.getline(user_input, max_size);

    cout << "You entered: " << user_input;

    return 0;
}


Code tags:
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Ok now I have fixed it. Thank you for showing me that. And I am talking about all of the code after the line:

cout << "Your length is " << length_in_feet << " in feet"

How do you use std::string in the code?
How do you use std::string in the code?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string> // include string

int main()
{
	// get in the habbit of using the std:: prefix for all you standard library stuff
	// instead of "using namespace std;"
	// i have been informed by more experienced programmers
	// that this is a better practice to use.
	
	std::string user_input;
	std::cout << "Please enter a phrase under 100 characters in length: ";
	std::getline(std::cin, user_input);
	std::cout << "You entered: " << user_input;

	return 0;
}


http://www.cplusplus.com/forum/beginner/132610/

BTW: you can include comments in your code tags. You do not have to exclude them like you did above.
Last edited on
So this is what I have right now, with a few adjustments, and it's almost working. When I run the program, it combines the output "Enter the room and rack where the cable begins." and "Enter the room and rack where the cable ends." It's outputting them both together, and only letting me input something for the both of them. And also, it isn't displaying anything on the final line of output, where the variables entered for location_1, location_2, and cable_type should be.

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
#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main ()

{
    double length_in_feet = 0;                                          // Variable for length in feet. Globally initialized

    {
    double length_in_meters = 0;                                        // Variable for length in meters locally initialized
    double sum = 0;
    const double constant = 3.2808;                                     // Constant for meters to feet conversion locally initialzed
    char indicator = 'y';                                               // Loop as long as 'yes' is entered
    
    while(indicator == 'y')                                             // Loop as long as 'yes' is entered
    {
        cout << "Enter measured length in meters"
             << endl;
        cin >> length_in_meters;                                        // Enter measured length of wire
        sum += length_in_meters;
        cout << endl
             << "Is there another cable length for you to enter? (enter y to enter another)? ";
        cin >> indicator;                                               // Read the indicator
        
    }
    
    length_in_feet = sum * constant;                                    // Conversion from meters to feet
    cout << "Your length is " << length_in_feet << " in feet"           // Output in feet
    << endl << endl;
    }
    
    {
        const unsigned MAX = 100;
        char location_1 [MAX] = {'\0'};
        
        cout << "Enter the room and rack where the cable begins.";
        cin.getline(location_1, MAX);                                   // Location where cable begins
        
    }
        
    {
        
        const unsigned MAX = 100;
        char location_2 [MAX] = {'\0'};
        
        cout << "Enter the room and rack where the cable ends.";
        cin.getline(location_2, MAX);                             // Location where cable is terminated
    }
        
    {
        const unsigned MAX = 100;
        char cable_type [MAX] = {'\0'};
        
        cout << "Enter the type of cable being used.";
        cin.getline(cable_type, MAX, '\n');                             // Type of cable being used
    }
        
    {
        const char MAX = 100;
        char location_1 [MAX];
        char location_2 [MAX];
        char cable_type [MAX];
        
        cout << "The distance between " << location_1 << " and " << location_2 << " is " << length_in_feet << " using " << cable_type << " cable.";
    }
    
    return 0;
    
}
You're using way too many brackets. You should include the string class if you want to use strings (which you didn't actually use any of). Global variables are variables initialized OUTSIDE of a function. Yours were all INSIDE of a function (main). Use '\n' instead of endl, it's faster.

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

using std::cin;
using std::cout;
using std::string;


const double constant = 3.2808; // Constant for meters to feet conversion


int main ( )
{
	double length_in_feet; // Variable for length in feet.
	double length_in_meters; // Variable for length in meters.
	double sum = 0;
	char indicator; // Loop as long as 'yes' is entered

	do {
		cout << "\nMeasured length in meters: ";
		cin >> length_in_meters; // Enter measured length of wire
		sum += length_in_meters;
		cout << "\nIs there another cable length for you to enter? (enter 'y' to enter another) ";
		cin >> indicator; // Read the indicator
		indicator = tolower ( indicator );
	} while ( indicator == 'y' ); // Loop as long as 'yes' is entered

	length_in_feet = sum * constant; // Conversion from meters to feet
	cout << '\n' << sum << " meters is " << length_in_feet << " feet.\n\n"; // Output in feet


	cin.clear ( ); cin.sync ( );

	
	string location_1, location_2, cable_type;
        
	cout << "Enter the room and rack where the cable begins.\n";
	getline ( cin, location_1 ); // Location where cable begins

        
	cout << "\nEnter the room and rack where the cable ends.\n";
	getline ( cin, location_2 ); // Location where cable is terminated

        
	cout << "\nEnter the type of cable being used.\n";
	getline ( cin, cable_type ); // Type of cable being used

        
	cout << "\nThe distance between " << location_1 << " and " << location_2
		<< " is " << length_in_feet << " feet using " << cable_type << " cable.\n\n";
}


I also added cin.clear() and cin.sync(). These two functions clear the input stream before we use getline(). Say someone had entered 'no' at the previous promt. We would read in only the first character, 'n'. "o\n" would still be in the stream, so when we went to use getline(), it would get those two characters as our answer to the question instead of letting us type in something new.
Last edited on
That fixed almost everything. The only thing that's still wrong is the last line of output. It's still outputting "Enter the room and rack where the cable begins." and "Enter the room and rack where the cable ends." as one output. Other than that, it's working perfectly

Then reason why line 38 appears to be skipped is because cin >> FOO leaves a newline in the buffer. getline() will not ignore this newline and read it. And, since it is a newline character, getline() will stop reading.
Yay295 also mentions this:
Yay295 wrote:
Say someone had entered 'no' at the previous promt. We would read in only the first character, 'n'. "o\n" would still be in the stream, so when we went to use getline(), it would get those two characters as our answer to the question instead of letting us type in something new.


31
32
//From Yay295's code
cin.clear ( ); cin.sync ( ); //C++ I/O has no way to "flush" the input buffer 

cin.clear() only resets the bitflags that tell you state of the stream, e.g. good or bad.
I am not familiar enough with std::istream::sync(), but I suggest the following:

31
32
33
34
35
36
37
38
39
40
41
42
//Removes all whitespace characters from the front of the buffer
cin >> ws;

//And to remove all invalid input (use either this or the above but not both)
cin.ignore(1000, '\n');
/* This third alternative below requires you include the <limits> header
cin.ignore(
    numeric_limits<streamsize>::max(), //Maximum number that can be read
    '\n' //The delimiter
);
*/
 


Last edited on
Topic archived. No new replies allowed.