Method Display Help

The code is supposed to display a written name and address using 30 *s. What little code we were given was mistakenly in Java (yes, it's homework, but don't jump down my throat yet. Keep in mind the book we were given gives no help and the teacher does not respond to e-mails. Never do programming online, people...). Using what little Google Fu I had, I managed to take it from Java to C++. After going down the list of errors it gave me, the last one it gives me is on line 6, "error: expected id-expression before 'int'." I looked online, but that error is real vague and I'm too limited to know what it means. Can anyone help me out with this last puzzle piece?



#include <iostream>
using namespace std;

class TestDisplay1
{
int string [ ], int args
{
string name, city, address, state, zip;

cout << "Enter name: " << endl;
getline (cin, name);

cout << "Enter address: " << endl;
getline (cin, address);

cout << "Enter city: " << endl;
getline (cin, city);

cout << "Enter state: " << endl;
getline (cin, state);

cout << "Enter zip code: " << endl;
getline (cin, zip);

int display ();

cout << "NAME: \n" << name;
cout << "ADDRESS: \n" << address;
cout << city << ", " << state << ", " << zip;

display ();

}
};
int display (int count)
{
int print;
for (count=0; count<30; count++)
{
print; '*';
}
print; "\n";
}
Last edited on
> yes, it's homework, but don't jump down my throat yet. Keep in mind (...)
blah blah blah

> The code is supposed to display a written name and address using 30 *s.
don't understand, please rephrase.

this will print the name and address inputted by the user
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main(){
	std::string name, address;
	std::cout << "Enter name: ";
	std::getline(std::cin, name);
	std::cout << "Enter address: ";
	std::getline(std::cin, address);

	std::cout << "Name: " << name << '\n';
	std::cout << "Address: " << address << '\n';

	return 0;
}

The end result is supposed to look like this:

******************************
John Smith
123 Some Road
Nowhere, KA, 55555
******************************

The idea is that the stars are supposed to be left alone under specific coding, the int display. Otherwise I'd have just used what you put and manually put in the stars.

I appreciate the guidance!
@Corbenik

Taking ne555's coding a little bit further, I've added in the rest of the input requests, then printed on screen the info, in the format you wanted. Hope this helps.

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

int main()
{
	std::string stars(30, '*');
	std::string name, address, city,state,zip;
	std::cout << "Enter name: ";
	std::getline(std::cin, name);
	std::cout << "Enter address: ";
	std::getline(std::cin, address);
	std::cout << "Enter city: " << std::endl;
    getline (std::cin, city);

    std::cout << "Enter state: " << std::endl;
    getline (std::cin, state);

    std::cout << "Enter zip code: " << std::endl;
    getline (std::cin, zip);
	
	std::cout << stars << std::endl;
	std::cout << name  << std::endl;
	std::cout << address  << std::endl;
	std::cout << city << ", " << state << " " << zip  << std::endl;
	std::cout << stars << std::endl;

	return 0;
}
Yes and no. I know I said that's what the end result is supposed to be, but I'm required to use a specific means. Here's an excerpt so you know what I'm dealing with:
"Write a static method named display. This method will return no value (i.e. will have void as the return type) and will receive no parameters. When called, the method display will display a line of 30 ‘*’ characters.

The method will use a For loop for displaying the characters. In each pass through the loop, it will display one ‘*’ character using the method System.out.print. After finishing the loop, it will output the newline ”\n” character .

The method call to display will be as below:

display( );"

As I later discovered, System.out.print is a Java script so I had to improvise, hence the code above. I think I have 90% of it right, but if it's janky I'd like to know where. That being said if white's meets this criteria, can you explain how? I'm not just trying to breeze through an assignment, but fully understand what I'm reading and writing.

Thanks, everyone, I really appreciate the guidance!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Write a static method named display.
// This method will return no value (i.e. will have void as the return type)
// and will receive no parameters.
// When called, the method display will display a line of 30 ‘*’ characters.
static void display()
{
    // The method will use a For loop for displaying the characters.
    for( int i = 0 ; i < 30 ; ++i )
    {
        // In each pass through the loop, it will display one ‘*’ character
        std::cout << '*' /* std::flush */ ;
    }

    // After finishing the loop, it will output the newline ”\n” character .
    std::cout << '\n' /* std::endl */ ;
}


The code using display() would be something like:
1
2
3
4
5
display() ; // call display to print a line of 30 asterisks at the top
print the name on this line
print the address on this line
print the city, state and zip on this line
display() ; // call display to print a line of 30 asterisks at the bottom 


The function display is abominably named; even a career-teacher should be able to do better than that.
Can I quote you on that, JL? lol That's the kind of thing I've been dealing with all semester.

I'm still getting the same line 6 error for "int string [ ], int args":
error: expected id-expression before 'int'

That seems to be the only error I'm encountering so far.

Thank you!
> Can I quote you on that, JL?

Yes.

Something like this, perhaps (this is C++, not Java):

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

// Write a static method named display.
// This method will return no value( i.e. will have void as the return type)
// and will receive no parameters.
// When called, the method display will display a line of 30 ‘*’ characters.
static void display()
{
    // The method will use a For loop for displaying the characters.
    for( int i = 0 ; i < 30 ; ++i )
    {
        // In each pass through the loop, it will display one ‘*’ character
        std::cout << '*' /* std::flush */ ;
    }

    // After finishing the loop, it will output the newline ”\n” character .
    std::cout << '\n' /* std::endl */ ;
}

static void test_display( std::string name, std::string address, std::string city,
                   std::string state, std::string zip )
{
    display() ;

    std::cout << name  << '\n' ;
    std::cout << address  << '\n' ;
    std::cout << city << ", " << state << ' ' << zip  << '\n' ;

    display() ;
}

int main()
{
    std::string name, address, city, state, zip ;

    std::cout << "Enter name: ";
    std::getline( std::cin, name );

    std::cout << "Enter address: ";
    std::getline( std::cin, address );

    std::cout << "Enter city: " ;
    std::getline( std::cin, city );

    std::cout << "Enter state: " ;
    std::getline( std::cin, state );

    std::cout << "Enter zip code: " ;
    getline( std::cin, zip );

    test_display( name, address, city, state, zip ) ;
}
That did the trick! Didn't work on the C++ Shell though for some reason.

I really appreciate the help on this!
Sorry for reopening this, but I hit a roadblock. It's an add-on to the assignment above only this time the header and footer aren't *'s but can be any character at any a number. I used an earlier code from an assignment and I keep getting the error code for Lines 13 and 25:
"error: expected primary-expression before '}' token"
I've never seen this before and I have no idea what it means. Google is way too vague about it. I thought it needed a ; to put a break on the count+, but that only made a new error. As I mentioned before my teacher isn't worth spit, so I don't know what I'm doing wrong. Can someone educate me properly on this error so I know how to fix it in the future? Furthermore, what would be the fix for these two lines?

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

static display1 ( char ch, int n)
{
    std::string ch1, count, n1;
    count = 1;
    while (count <= n1)
    {
       std::cout << ch1;

       count = count+
    }
}

static display2 ( char ch, int n)
{
    std::string ch2, count, n2;
    count = 1;
    while (count <= n2)
    {
       std::cout << ch2;

       count+
             }
}

static void test_display( std::string name, std::string address, std::string city,
                   std::string state, std::string zip )
{
    char ch1, ch2;
    int n1, n2;
    display1(ch1, n1) ;

    std::cout << name  << '\n' ;
    std::cout << address  << '\n' ;
    std::cout << city << ", " << state << ' ' << zip  << '\n' ;

    display2(ch2, n2) ;
}

int main()
{
    std::string name, address, city, state, zip, ch1, ch2, n1, n2 ;

    std::cout << "Enter the character to be used in the header: \n";
    std::getline( std::cin, ch1);

    std::cout << "Enter the number of characters to be printed in the header line: \n";
    std::getline( std::cin, n1);

    std::cout << "Enter your full name: \n";
    std::getline( std::cin, name );

    std::cout << "Enter your street address: \n";
    std::getline( std::cin, address );

    std::cout << "Enter your city: \n";
    std::getline( std::cin, city );

    std::cout << "Enter your state: \n";
    std::getline( std::cin, state );

    std::cout << "Enter your zip code: \n";
    getline( std::cin, zip );

    std::cout << "Enter the character to be used in the footer: \n";
    std::getline( std::cin, ch2);

    std::cout << "Enter the number of characters to be printed in the footer line: \n";
    std::getline( std::cin, n2);

    std::cout << "\n";

    test_display( name, address, city, state, zip ) ;
}
closed account (48T7M4Gy)
I noticed you are mixing up strings and int's.
All you need do is combine the two
Last edited on
That's the trouble. My teacher meshes Java with C++, and for a newbie like me, I can't tell it apart easily like the rest of you. For that, I'm seriously grateful when you guys point me in the right direction, like now! :)

I'll keep what you said in mind and give this a go.

Thank you so much!
I tried shrinking down everything and got rid of the statics as suggested. The header works like I want it to to, but the bad news is that everything below it skips entirely and inputs odd digits and characters. I tried messing around with the numbering in the displays, but it only made it worse. Here's the code before I started monkeying around with it. Sorry for being a hassle, but I just don't understand how something that worked, suddenly doesn't.

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

void display1( char ch, int n )
{
for(int i = 0; i < n; ++i)
std::cout << ch;
std::cout << '\n';
}

void display2( char ch, int n )
{
for(int i = 0; i < n; ++i)
std::cout << ch;
std::cout << '\n';
}

void test_display( char name, char address, char city, char state, char zip, char ch1, char ch2, int n1, int n2 )
{
display1( ch1, n1 ) ;

std::cout << name << '\n' ;
std::cout << address << '\n' ;
std::cout << city << ", " << state << ' ' << zip << '\n' ;

display2( ch2, n2 ) ;
}

int main()
{
char name;
char address;
char city;
char state;
char zip;
char ch1;
char ch2;
int n1 = 0;
int n2 = 0;

std::cout << "Enter the character to be used in the header: \n";
std::cin >> ch1;

std::cout << "Enter the number of characters to be printed in the header line: \n";
std::cin >> n1;

std::cout << "Enter your full name: \n";
std::cin >> name;

std::cout << "Enter your street address: \n";
std::cin >> address;

std::cout << "Enter your city: \n";
std::cin >> city;

std::cout << "Enter your state: \n";
std::cin >> state;

std::cout << "Enter your zip code: \n";
std::cin >> zip;

std::cout << "Enter the character to be used in the footer: \n";
std::cin >> ch2;

std::cout << "Enter the number of characters to be printed in the footer line: \n";
std::cin >> n2;

std::cout << "\n";

test_display( name, address, city, state, zip, ch1, ch2, n1, n2 ) ;
}
closed account (48T7M4Gy)
Why don't you test it with a simple set of inputs instead of all that rubbish as I suggested before?

For a start, you don't need 2 display functions for the lines. You would know this if you had bothered to read the code I gave you earlier. Just send the two parameters ch and n to the same display_line function. That's what functions are for - to save work, not make it!

Second, name address etc aren't char's, they're strings.

I suggest you state specifically and clearly - what is the input for this latest, what is the output and what do you expect the program to do.

As it stands a few of us have given you sufficient information for you to debug your program or start again from your initial post. There is little or no point repeating the advice you've been given.

It's in your interests to go through what you have already been given and apply it. There's no harm in tinkering and fixing up what you have is fairly simple.

I did, kemort, but I don't know what you meant by "simple set." Another problem is that's not how my good for nothing teacher wants it. The reason it's so long is because he wants us to show "step by step" how the code works.

I thought i stated specifically and clearly but I'll try just keep going solo on this. I'm clearly being a bother and that's not my intention. My current problem is now this:
"The header works like I want it to to, but the bad news is that everything below it skips entirely and inputs odd digits and characters. I tried messing around with the numbering in the displays, but it only made it worse."

The end result is supposed to be like this:
Enter the character to be used in the header:
& (as an example)
Enter the number of characters to be printed in the header line:
5
Enter your full name:
John Doe
Enter your street address:
123 Thatthere Road
Enter your city:
Nowhere
Enter your state:
KS
Enter your zip code:
00000
Enter the character to be used in the footer:
@
Enter the number of characters to be printed in the footer line:
3

Ideally it should show this:
&&&&&
John Doe
123 Thatthere Road
Nowhere, KS, 00000
@@@

But it after I input the name, it skips the other entries entirely and goes straight to the end result:
Enter the character to be used in the header:
&
Enter the number of characters to be printed in the header line:
5
Enter your full name:
John Doe
Enter your street address:
Enter your city:
Enter your state:
Enter your zip code:
Enter the character to be used in the footer:
Enter the number of characters to be printed in the footer line:

&&&&&
J
o
h, n D

Even when I put a std::string for the address, it still skips it to zip (which I set to Int).

As I said before, I'm thrown off, because all the codes are the same. I don't understand how the header input and name, both under char are not affected, but the rest are.

I realize this is super elementary to you, but it's not to me. I'm really sorry if this is a bother to you all, but since I can't find answer through my school or online, I have to rely on you. And don't ask me to compact it any more, because I truly don't know how without making it worse. The code above is the most stable I can make it at the moment.
closed account (48T7M4Gy)
You can look up works in the tutorials/reference on this site
Last edited on
That did the trick!

Thank you so much for the help and for understanding. I am so relieved to finally understand what the heck I was writing.
Topic archived. No new replies allowed.