for Statement Issues - noob issues

Hey guys,

I can't get my output on the format below. HELP! Whats the issue with my code?

CeErre


What is the speed of the vehicle in mph?
40 How many hours has it traveled? 3
Hour Distance Traveled
--------------------------------
1 40
2 80
3 120


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

using namespace std;

//Funtions
void velocidad (float &speed1);
void tiempo (float &time1);
void calc (float &distance1, float time1, float speed);
void impresion (float i1, float time1, float &distance1);

// Sentinel
int sentinel;

int main()
{
    do{
    float distance, speed, time,i;
    i= time;

    cout<<"This program will calculate distance traveled.\n";
    cout<<"ONLY positive number will be accepted for calculations\n\n";
    
    velocidad (speed);              //gets me the speed
    tiempo (time);                  //gets me the time
    calc (distance,time,speed);     //calculates
    impresion (i, time, distance);  //prints
    
    cout <<"Your calculated distance is: "<<distance<<endl;
    
    cout << "Press 0 to exit or any number to continue: ";
    cin>> sentinel;
    
    }while (sentinel != 0);
    return 0;
}

void velocidad (float &speed1)
{
    do{
    cout<<"Please enter the speed (miles): ";
    cin >> speed1;
    }
    while (speed1 <0);
}
void tiempo (float &time1)
{
    do{
    cout << "\nPlease enter the time (hours): ";
    cin >> time1;
    }
    while(time1 <1);
}
void calc (float &distance1, float time1, float speed1)
{
    distance1 = speed1 * time1;
}
void impresion (float i1, float time1, float &distance1)
{
    cout << "Hour" << setw(20) << "  Distance Traveled" << setw(10) << endl;
    for (i1 = 0; i1 < time1; i1++) {
        cout  << time1 <<  setw(15) << distance1 << setw(10) << endl;}
}
Last edited on
> Hey geeks,
Yeah, not the best salutation.
Personally, I find it mildly offensive (even more so when I read your other posts).

> I can't get my output on this format. HELP! What the issue with my code?
Lemme guess, you used floating point, so you have
- lots of rounding .9999 and .0001 approximations of what you expected.
- A loop that blows up because you tried to use == somewhere.

So where is your code for these 4 functions?
1
2
3
4
void velocidad (float &speed1);
void tiempo (float &time1);
void calc (float &distance1, float time1, float speed);
void impresion (float i1, float time1, float &distance1);
Hello rodriguez135994,

When I tried to compile the code it did not.

First you are missing 4 functions.

Your variables float distance, speed, time,i; are all uninitialized. This causes an error when you try to use them. On my computer these all have the garbage value if "-107274176.". Which makes the next line -107274176. = -107274176.. Not much point to that.

You defined a variable "i", but what about "p" and "q"?

Line 17 should be inside "main" where it is the only place it is used. Otherwise it could be a potential problem. IMHO "sentinel" is a poor choice for the variable name. Something like "cont" or "doAgain" makes more sense.

Andy
Hey Guys;

I apologize (ive edited my original entry, even change the salutation -salem c-) for the entry, I was missing some code. My intention is to make it print table style. Help would be greatly appreciated!

C
> calc (distance,time,speed); //calculates
You should call this inside your loop, with values for i1

1
2
3
4
5
    for (i1 = 0; i1 < time1; i1++) {
        calc (distance,i1,speed);
        cout  << time1 <<  setw(15) << distance1 << setw(10) << endl;
    }
}


> void impresion (float i1, float time1, float &distance1);
1. declare i1 as a local variable, not a parameter.
2. pass in the speed to make the above work.
Hello CeErre,

It is best not to change the original post, but to add or show any changes in a reply. Changing the original post could make it look like there is nothing wrong.

Your code:
1
2
3
4
5
6
7
8
9
// Sentinel
int sentinel;  // <--- Global variables like this should be avoided.

int main()
{
    do
    {
        float distance, speed, time, i;  // <--- Uninitialized variables especially "i".
        i = time;  // <--- Is there a point to this? It is still setting i = garbage. 


Should look like this:
1
2
3
4
5
6
7
8
9
10
int main()
{
    // Sentinel
    int sentinel;  // <--- Only used in "main". No need for a global variable. But it does need defined outside of the do/while loop.

    do
    {
        double distance{}, speed{}, time{}, i{};

        cout << "This program will calculate distance traveled.\n";


There is more to cover, but I need to take a break first.

Andy
Unless you're been told to use these functions with the given declarations, its easier to have the input functions return a value and for impresion to display and calculate - as the distance for each time period is simply the value of the last period plus the speed.

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

using namespace std;

float velocidad();
float tiempo();
float impresion(float s1, float time1);

int main()
{
	int sentinel {};

	do {
		cout << "This program will calculate distance traveled.\n";
		cout << "ONLY positive number will be accepted for calculations\n\n";

		const float speed {velocidad()};
		const float time {tiempo()};
		const float distance {impresion(speed, time)};

		cout << "Your calculated distance is: " << distance << endl;
		cout << "Press 0 to exit or any number to continue: ";
		cin >> sentinel;

	} while (sentinel != 0);

	return 0;
}

float velocidad()
{
	float speed1 {};
	do {
		cout << "Please enter the speed (miles): ";
		cin >> speed1;
	} while (speed1 < 0);

	return speed1;
}

float tiempo()
{
	float time1 {};

	do {
		cout << "\nPlease enter the time (hours): ";
		cin >> time1;
	} while (time1 < 1);

	return time1;
}

float impresion(float s1, float time1)
{
	float dist {};

	cout << left << setw(10) << "Hour" << setw(20) << "Distance Travelled" << endl;

	for (float i1 = 0; i1 < time1; ++i1) {
		dist += s1;
		cout << left << setw(10) << i1 + 1<< setw(20) << dist << endl;
	}

	return dist;
}




This program will calculate distance traveled.
ONLY positive number will be accepted for calculations

Please enter the speed (miles): 40

Please enter the time (hours): 3
Hour      Distance Travelled
1         40
2         80
3         120
Your calculated distance is: 120
Press 0 to exit or any number to continue: 0

SeePlus;

That worked perfectly! Truly grateful! Thanks for the tips on how to behave in the forums. Happy holidays!

CeErre
Topic archived. No new replies allowed.