Iterative Loops And Small Problems #1-3

Pages: 123
You need a displayLabel() method.
closed account (oG8qGNh0)
I have no idea what that means
You add the method to the Package class you wrote.
First write the code to do this:

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.


Hint: the variable should be an int.

After the user enters the number, print it out to make sure you have what you think you have.

Don't do anything else until this works.

Then do the next step.

Always do 1 small step at a time. Don't ever try to code the whole program at 1 time.
closed account (oG8qGNh0)
int boxes;
if (boxes<2)
print error message
Last edited on
int boxes;
if (boxes<2)
print error message


3 lines, one of which is not even a line of C++, is not a program.

Write a program to do the first step. When I wrote "Don't do anything else until this works", I meant: Write a program to do this, compile it, and run it. Don't try to write any more code until what you wrote does what you want it to do. I'm sorry if I was too subtle.

Nobody here is going to do your homework for you.

Start with this:

1
2
3
4
int main()
{
    // put your code here
}


If you want ot post your code again, post the whold program, not just 3 random line.

Questions about what you did post:

Why are you trying to print an error message? The first step is only to ask for a user's input and then print it out. No error handling. No printing of error messages. Just make sure you can correctly get input.

Why should I be unable to send just 1 box?

closed account (oG8qGNh0)
So I am supposed to make #3 a for loop and its not running the way I want it to...

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

for (int student = 10; student <= 10; student++)
{
cout << student << endl;
}

{
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);
}
is 10 + 1 greater than or less than 10?
closed account (oG8qGNh0)
greater than 10. This code just looped 1 time
Last edited on
and now you know why.......
You can do #1 like this:

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <streambuf>
#include <cctype>
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
#include <limits>

class Char {
public:
	Char(std::istream& _Istr = std::cin) : Istr(_Istr) {}

	char operator()() {
		return (char)Istr.rdbuf()->sbumpc();
	}

private:
	std::istream& Istr;
};

class Word {
public:
	using Word_t = std::vector<char>;

	Word(std::istream& _Istr = std::cin) : mychr(Char(_Istr)) {}

	auto operator()() {
		char ch {};
		Word_t vc;

		while (std::isspace(ch = mychr()) && ch != '\n');

		if (ch != '\n') {
			vc.push_back(ch);

			while (!std::isspace(ch = mychr()))
				vc.push_back(ch);
		}

		return std::pair(vc, ch == '\n');
	}

private:
	Char mychr;
};

std::ostream& operator<<(std::ostream& os, const Word::Word_t& wd)
{
	for (const auto w : wd)
		os << w;

	return os;
}

class Sentence {
public:
	using Sent_t = std::vector<Word::Word_t>;

	Sentence(std::istream& _Istr = std::cin) : mywrd(Word(_Istr)) {}

	auto operator()()
	{
		Sent_t wrds;

		for (bool notnl = false; !notnl; ) {
			const auto [w, nl] = mywrd();

			Word::Word_t wd;

			for (const auto ww : w)
				wd.push_back(ww);

			if (!wd.empty())
				wrds.emplace_back(wd);

			notnl = nl;
		}

		return wrds;
	}

private:
	Word mywrd;
};

std::ostream& operator<<(std::ostream& os, const Sentence::Sent_t& sen)
{
	for (const auto& s : sen)
		os << s << " ";

	return os;
}

class Address {
public:
	using Address_t = std::vector<Sentence::Sent_t>;

	Address(std::istream& _Istr = std::cin) : myaddr(Sentence(_Istr)) {}

	auto operator()()
	{
		Address_t adds;

		for (Sentence sent; true; ) {
			const auto s {sent()};

			if (s.empty())
				break;

			adds.emplace_back(s);
		}

		return adds;
	}

private:
	Sentence myaddr;
};

std::ostream& operator<<(std::ostream& os, const Address::Address_t& addr)
{
	for (const auto& a : addr)
		os << a << '\n';

	return os;
}

int main()
{
	int numberboxes {};

	while (((std::cout << "How many boxes: ") && !(std::cin >> numberboxes)) || numberboxes < 1) {
		std::cout << "Invalid input\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	Address adds;
	std::ostringstream  oad;

	std::cout << "Enter Mailing Address (<CR> to terminate)\n";
	oad << adds();

	for (int n = 0; n < numberboxes; ++n)
		std::cout << oad.str() << "BOX NUMBER " << n + 1 << " OF " << numberboxes << "\n\n";
}


See https://repl.it/repls/CoarsePungentFtpclient#main.cpp


How many boxes: 2
Enter Mailing Address (<CR> to terminate)
BOB & PATRICK'S SEASHELL EMPORIUM
UNDER THE SEA, MD 12345

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


:) :) :D :D :D :cheek_tongue:




closed account (oG8qGNh0)
No simpler way to do it?
Of course.......

See https://dictionary.cambridge.org/dictionary/english/tongue-in-cheek for definition of tongue-in-cheek (adjective).
closed account (oG8qGNh0)
Why would you send me that tongue-in-cheeck definition?
closed account (oG8qGNh0)
What should I delete to make it more simple?
Maybe you can simplify things if you re-write the program for just one label and photocopy the rest.
Nearly all of it....

It can be done in 11 lines (excluding #include statements).

The definition is apt for my previous code for #1.......

Rather than making fun of problem #1 it would be better to help out. Especially if @OP doesn’t have access to a photocopier. Maybe use carbon paper, take a phone picture or re-run the program, run the program simultaneously on multiple computers. How else to help out @OP?
Aw sucks...... spoilsport....

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

int main() {
	int numberboxes {};
	std::cout << "How many boxes: ";
	std::cin >> numberboxes;
	std::cin.ignore();
	std::cout << "Enter Mailing Address (<CR> to terminate)\n";
	std::vector<std::string> addr;
	for (std::string s; std::getline(std::cin, s) && !s.empty(); addr.emplace_back(s));
	for (int n = 0; n < numberboxes; std::cout << "BOX NUMBER " << ++n << " OF " << numberboxes << "\n\n")
		std::copy(addr.begin(), addr.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}



How many boxes: 2
Enter Mailing Address (<CR> to terminate)
BOB & PATRICK'S SEASHELL EMPORIUM
UNDER THE SEA, MD 12345

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

closed account (oG8qGNh0)
Bro what are you talking about? First you say tongue-in-cheeck and then spoilsport... this world has become a disaster
Pages: 123