Iterative Loops And Small Problems #1-3

Pages: 123
closed account (oG8qGNh0)
PROBLEM #1

Bob & Patrick’s Seashell Emporium uses mailing labels for shipping boxes. Write a program to display a mailing label for each box to be shipped. The program output should look similar to:

How many boxes? 2
BOB & PATRICK’S SEASHELL EMPORIUM
UNDER THE SEA, MD 12345
BOX NUMBER 1 OF 2

BOB & PATRICK’S SEASHELL EMPORIUM
UNDER THE SEA, MD 12345
BOX NUMBER 2 OF 2

PROBLEM #2

Write a program that asks the user for starting and ending values and then displays a table of squares and cubes. The program output should look similar to:

Enter starting value: 5
Enter ending value: 12

X X SQUARED X CUBED
5 25 125
6 36 216

12 144 1728

PROBLEM #3

Write a program that reads in letter grades for a class of 10 students and displays the number of students who passed (D or better) and the number who failed (E). The program output should look similar to:

Enter a grade: C
Enter a grade: A
…additional inputs not shown here…
6 students passed
4 students failed

I am confused on how to do no. 1

Problem 2 Solution: (Please check)!
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 <cmath>
using namespace std;

//printTabs function to calculate and output table
void printTabs(int startValO, int rowO)
{

int answer;

for(int i = 1; i <= rowO ; i++)
//start loop to control rows
{

for(int j = 1; j <= 3; j++)
//loop to control columns
{
answer = pow(startValO, j);
//expression to calculate square and cube of starting value/incremented value with column j

cout << answer << '\t';
//out put of integer answer
       
}
//end of column loop

cout << endl;
}
//end of row loop
}
//End of printTabs


//selTabs to 
void selTabs(int startValP, int rowP)
{
//if statement to set increment to 1 if a value of 0 or less us input

        
printTabs(startValP, rowP);
}

//main function to call table from function printTabs
int main()
{

int startVal, rows;

//user instructions and input
cout << "Enter starting value: ";
cin >> startVal;
cout << "Enter ending value: ";
cin >> rows;



//display headings
cout << "NUMBER" << '\t' << "SQUARE" << '\t' << "CUBE" << endl;
//call and produce table from void function printTabs
selTabs(startVal, rows);

return 0;

}


Problem 3 Solution: (Please check)!

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
#include<iostream>
using namespace std;
int main()
{
int grade;
int passing=0;
int failing=0;

cout << "Enter a grade: ";
cin >> grade;
if (grade== 'a')
{
passing++;
}
else if (grade== 'b')
{
passing++;
}
else if (grade== 'c')
{
passing++;
}
else if (grade== 'd')
{
passing++;
}
else if (grade== 'e')
{
failing++;
}
cout << "Please enter a valid choice" << endl;

}
cout << passing << " students passed" << endl;
cout << failing << " students failed" << endl;
system("pause");
return(0);
}


*Remember USING REPL.IT
Last edited on
I am confused on how to do no. 1


These 3 questions are totally unrelated to each other. Why did you throw in questions 2 and 3 into this post when you have questions about #1.

What about #1 confuses you?

The "labels" you are to print are the return address labels with a counter. It is extremely straight forward. What do you not understand? If you say "I don't know" or "all of it", I'll know I'm talking to my own kids, and will lose all patience.

(Please check)!

Problem 3 doesn't even compile. Neither program does what it is supposed to do. Why haven't you tried running the program and seeing if it works before bringing it to us? And don't ask us what's wrong, tell us what's wrong and tell us what you don't understand.
Last edited on
Problem 2 Solution: (Please check)!

Fail Doesn't increment number.

Problem 3 Solution: (Please check)!

Fail Doesn't compile. No loop.

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

using namespace std;

int main()
{

    cout << "How many boxes? 2" << endl;
    cout << "BOB & PATRICK’S SEASHELL EMPORIUM" << endl;
    cout << "UNDER THE SEA, MD 12345" << endl;
    cout << "BOX NUMBER 1 OF 2" << endl;
    cout << endl;
    cout << "BOB & PATRICK’S SEASHELL EMPORIUM" << endl;
    cout << "UNDER THE SEA, MD 12345" << endl;
    cout << "BOX NUMBER 2 OF 2" << endl;

    return 0;
}
Last edited on
closed account (oG8qGNh0)
Okay #1 cannot be that simple... can it? and what do you mean by doesn't compile. No loop.??
Last edited on
Okay #1 cannot be that simple... can it?

To be fair to you, it's probably not that simple. I reckon if you submit it you'd probably get 4/10.

But the rules of this game are "You need to put some skin in the fight too! ", as they say in the movies.

Let's see you use an int, a cout, a cin, and a for loop to do the job with what's there so far.
and what do you mean by doesn't compile. No loop.??


The code as provided in the post doesn't compile. The compiler displays an error. As you're using repl.it, the first error is

main.cpp:34:1: error: unknown type name 'cout'
cout << passing << " students passed" << endl;


https://repl.it/repls/CluelessWhitesmokeVariety#main.cpp

reads in letter grades for a class of 10 students


To read in the grades for 10 students, you either need a loop to read the grade for each student (good) or explicit code to read grades for 10 students (bad).

PS I rather like this name given by repl.it
Last edited on
closed account (oG8qGNh0)
againtry (its 6 points not 10)
but anyways.

Problem 2

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

//printTabs function to calculate and output table
void printTabs(int startValO, int rowO, int incrementO)
{
int answer;

for(int i = 1; i <= rowO ; i++)
//start loop to control rows
{

for(int j = 1; j <= 3; j++)
//loop to control columns
{
answer = pow(startValO, j);
//expression to calculate square and cube of starting value/incremented value with column j

cout << answer << '\t';
//out put of integer answer
}//End of row loop

startValO += incrementO;
// to increment the initial value 
cout << endl;

}//End of row loop
}//End of printTabs


//selTabs to 
void selTabs(int startValP, int rowP, int incrementP)
{
//if statement to set increment to 1 if a value of 0 or less us input
if(incrementP < 1)
incrementP = 1;
        
printTabs(startValP, rowP, incrementP);
}

//main function to call table from function printTabs
int main()
{
int startVal, rows, increment;

//user instructions and input
cout << "Enter starting value: ";
cin >> startVal;
cout << "Enter the number of values to be displayed: ";
cin >> rows;
cout << "Enter the increment between the values: ";
cin >> increment;


//display headings
cout << "NUMBER" << '\t' << "SQUARE" << '\t' << "CUBE" << endl;
//call and produce table from void function printTabs
selTabs(startVal, rows, increment);

return 0;

}


For problem 3 I dunno how to loop so it gets all 10 sudents
Last edited on
problem 2

Well it now produces correct output. 7/10. Why selTabs() function - increment check can go in printTabs()? Why loop j = 1? pow(startValo, 1) is just startVal0? So print the number outside of the j loop and start j at 2. Also the displayed numbers don't align with the headings.

problem 3.

As you say you're proficient with python, then you must know/understand loops. If you're unsure re the C++ syntax for loops, look at previous examples to see use.
Last edited on
againtry (its 6 points not 10)
but anyways.

LOL,
So far you'll get 0.4 * 6 = 2.4 out of 6, let's make it 2. I get 1 you get 1.
closed account (oG8qGNh0)
AH HAA Got it finally!

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
#include<iostream>
using namespace std;
int main()
{
string grade;
int passing=0;
int failing=0;
int student=10;

while (student>0) {
cout<<"Enter a grade: ";
cin>>grade;
student--;
if (grade== "a")
passing++;
else if (grade== "b")
passing++;
else if (grade=="c")
passing++;
else if (grade=="d")
passing++;
else if (grade=="e")
failing++;
}

cout<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
return(0);
}
grade is an int.
"a" is a const char[], or C-style string.
'a' is a const char.

An int can be compared to a char (const or non-const), but not to a C-style string.

Try:

if (grade == 'a')
closed account (oG8qGNh0)
grade is a char actually

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
#include<iostream>
using namespace std;
int main()
{
char grade;
int passing=0;
int failing=0;
int student=10;

while (student>0) {
cout<<"Enter a grade: ";
cin>>grade;
student--;
if (grade== 'a')
passing++;
else if (grade== 'b')
passing++;
else if (grade== 'c')
passing++;
else if (grade== 'd')
passing++;
else if (grade== 'e')
failing++;
}

cout<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
return(0);
}
grade is a char actually


It was a string in the previous post.

That is one way of doing it which works - just, but it's not very good code. If statements can use || for logical or and && for logical and. In this case a for loop is also a better choice than a while loop.

There is also no checking for invalid grade entered or to deal with upper-case/lower-case input. The original problem used upper-case letters.
Last edited on
It was a string in the previous post.


I'm pretty sure is was an int in a previous post, too, that was subsequently deleted. It was the post that asked why specific lines had red syntax error squiggles.

But when posts get deleted, following posts lose their context.
closed account (oG8qGNh0)
Basically
cout << "How many boxes? 2" << endl;

Instead of hard coding that two in the line above, make a variable to hold it and ask the user the question, then let them type in the response.

Once you have it, you will run a loop that many times - take a peek at the slides about for loops - there is probably an example in there really similar to what you need.

Then as the loop runs, you will print this - once inside the loop

cout << "BOB & PATRICK’S SEASHELL EMPORIUM" << endl;
cout << "UNDER THE SEA, MD 12345" << endl;

on this last line, you will fill in with the variable controlling your loop where the 1 is, and with the variable you originally read the user input into where the 2 is
cout << "BOX NUMBER 1 OF 2" << endl;
Sounds good to me.
So now write the corresponding code for each step.
closed account (oG8qGNh0)
my code... I have no idea how to correct it like that.

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
#include <iostream> 
#include <string> 
using namespace std; 
 
class Package 
{ 
private:  
string boxes, state; 
int zip; 
string address; 

                     
public: 
void setBoxes(string boxes); 
void setState(string state); 
void setZip(int zip); 
void setAddress(string address); 
string getboxes()
{ 
return boxes;
} 
    
string getState()
{
return state;
} 
int getZip()
{
return zip;
} 
string getAddress()
{
return address;
} 
	
	 
                                
};
void Package::setState(string s)
{
state = s;
} 
void Package::setZip (int zp)
{
zip = zp;
} 
void Package::setAddress(string adr)
{
address = adr; 
} 

                                         
int main()
{
int i;  
string numberboxes,customerAddress,state;  
string customerState; 
int customerZipcode;
	         
cout<<"How many boxes? "<<endl; 
cin>>numberboxes; 
cout<<endl; 
     
         
cout<<"Enter Customer Address"<<endl; 
cin>>customerAddress; 
cout<<endl; 
  
cout<<"Enter Customer State"<<endl; 
cin>>customerState; 
cout<<endl; 
    
         
cout<<"Enter Customer ZIP code"<<endl; 
cin>>customerZipcode; 
cout<<endl; 
    
return 0;
}
The code you posted has absolutely nothing to do with the steps YOU delineated two posts earlier.

You have an int variable named i that is never used, and a string variable named numberBoxes that is, as I said, a string, so can't be used for counting.

You ask for customer address, but you are printing return mailing labels, so the customer address is meaningless.

Why don't you show code for the following?

Basically
cout << "How many boxes? 2" << endl;

Instead of hard coding that two in the line above, make a variable to hold it and ask the user the question, then let them type in the response.

Once you have it, you will run a loop that many times - take a peek at the slides about for loops - there is probably an example in there really similar to what you need.

Then as the loop runs, you will print this - once inside the loop

cout << "BOB & PATRICK’S SEASHELL EMPORIUM" << endl;
cout << "UNDER THE SEA, MD 12345" << endl;

on this last line, you will fill in with the variable controlling your loop where the 1 is, and with the variable you originally read the user input into where the 2 is
cout << "BOX NUMBER 1 OF 2" << endl;
closed account (oG8qGNh0)
this is confusing...
Pages: 123