funtion help

I am needing help on a problem I have for school. I am not asking anyone to do this for me but help me understand what I am doing and how to go about doing this.

So the homework is to take a word (we will use TIGER) for right now. and output it like this

TTTTTTTTTTTTTTTTTTT
IIIIIIIII
IIIIIIIII
GGGGGGG
GGGGGGG
GGGGGGG
EEEEE
EEEEE
EEEEE
EEEEE
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR

The number the letter is in the word (t is the first letter) so it is output in one row but since it is the 20th letter in the alephbet it is outputted 20 times.

We have to use two funtions and a main. So far I have osmehtin like this

# include <iostream>
# include <fstream>
# include <iomanip>
using namespace std;

ifstream fin;

void line (char ch, int num)
{
int i;
for (i = 1; i <= num; i++)
cout <<ch;
cout<<endl;
}

void rectangle (char ch, int x, int y)
{

}



int main (void)
{
char ch;
int count = 0, column;
ifstream fin;
fin.open("animals.txt");
fin.get(ch);

while(!fin.eof())
{
if (ch!='/n')
{
count++;
column = ch - 'a' + 1;
rectangle(ch,column,0);
}



cout<<ch<<endl;


}
}


What I am stuggling with is how to I move it from main to rectangle and then from rectangle to line. I just don't know what to do. Any help would be great I can answer any questions.
Things to think about.

1. When calculating column, make sure you are using either uppercase or lowercase. If you perform ('T' - 'a'), you will not get what you expect. Try toupper() or tolower().

2. The y value in your call to rectangle is should be the letter's location in the word, which is your variable count.

3. Your rectangle function should call line y times. There will need to be a loop here to count the lines that are printed out.

4. When posting code in the forum, use code tags. Click the "<>" button in the Format menu and post your code between the tags.

Did that answer your questions?
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 <fstream>
# include <iomanip>
using namespace std;

ifstream fin;

void line (char ch, int num)
{
	int i;
	for (i = 1; i <= num; i++)
	cout <<ch;
	cout<<endl;
}

void rectangle (char ch, int x, int y)
{
	line(ch,x);
	  
}

int main (void)
{
char ch;
int count = 0, column;
ifstream fin;
fin.open("animals.txt");
fin.get(ch);

while(!fin.eof())
{
	if (ch!='/n')
	{
		count++;
		column = ch - 'A' + 1; 
		rectangle(ch,column,count);
	}
	
	else
	{
		count=0;
		cout<<endl;
		
	}
	
	fin.get(ch);
	


}
}



ok so this works for the most part but like with CAT in the first post I cant get it to count the letter that it is in the word and make that many rows. there should be 1 row for c's 2 for a's and 3 for t's.

CCC
A
A
TTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTT

any suggestions?
3. Your rectangle function should call line y times. There will need to be a loop here to count the lines that are printed out.
so something along the lines of

1
2
3
4
5
6
7
8
9
10

void rectangle (char ch, int x, int y)
{
int number = 1;

	while( number <= count)
 line(ch,x);
	  
}


count is found in the main funtion
Since count is found in the main function, you can't use it here. However, count was passed into the rectangle function through the variable y, so you can use that variable here.

The loop you wrote in lines 6 and 7 are an infinite loop--it will never end. Nothing is changing the value number or the value count, so number will always be <= count. Line 7 will execute over and over and over without ending.

There are a number of ways to provide the loop. Using the while loop, you can do:

1
2
3
4
5
6
7
8
9
void rectangle (char ch, int x, int y)
{
    int number = 1;
    while(number <= y)    // correct variable here
    {                      // Curly braces allow multiple statements in while loop
        line(ch,x);
        number++;          // Increment the counter
    }  
}


In your original post, lines 11 and 12 are a 'for' loop. You could use the same type of loop here.

1
2
3
4
5
6
7
void rectangle (char ch, int x, int y)
{
    for (int i = 0; i < y; i++)
    {
        line(ch,x);
    }  
}


A few style things to note:

(1) Typical C and C++ usage is to start loops at 0 and end when they reach the threshold (for (int i = 0; i < y; i++)) rather than looping from 1 through the threshold (for (int i = 1; i <= y; i++). This will come in especially handy when you learn more about indexing into arrays. You should probably get into the habit of this now (see lines 11 and 12 of original post).

(2) In C++ you can declare a variable inside a 'for' statement that is scoped exclusively to the for loop. This allows you to do for (int i = 0; i < y; i++) rather than int i; for (i = 0; i < y; i++). If the counter is not needed after the loop exits, you can declare the counter when you need it.

(3) It is usually a good idea to add curly braces around the content of an if/for/while/etc. block, even if it is only a single statement. As you write more advanced code, you will get to points where you want to add or remove statements from an if/for/while/etc., and remembering to add or remove curly braces can become a hassle. Plus the curlys can be used to better visually show program structure.
ok I see. I had something like that in the rectangle function but I got rid of it because I it was messing up and I thought it was wrong. Everything works except I can not get my count to reset.

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

ifstream fin;
ofstream fout;

void line (char ch, int num)
{
	int i;
	for (i = 1; i <= num; i++)
	fout <<ch;
	fout<<endl;
}

void rectangle (char ch, int x, int y)
{
	for (int bounce = 0; bounce < y; bounce++)
	line(ch,x);
	
	
}

int main (void)
{
char ch;
int count = 0, column;
ifstream fin;
fin.open("animals.txt");
fout.open ("animalresults.txt");
fin.get(ch);

while(!fin.eof())
{
	if (ch!='/n')
	{
		count++;
		column = ch - 'A' + 1; 
		rectangle(ch,column,count);
	}
	
	else
	{
		
		fout<<endl;
		count=0;
	}
	
	fin.get(ch);
	


}
}


in the else loop it should reset to 0 but it is not
What input are you using and what output are you getting?

And are you doing this in Windows or on a *nix machine?
Last edited on
TTTTTTTTTTTTTTTTTTTT
IIIIIIIII
IIIIIIIII
GGGGGGG
GGGGGGG
GGGGGGG
EEEEE
EEEEE
EEEEE
EEEEE
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR






DDDD
DDDD
DDDD
DDDD
DDDD
DDDD
DDDD
OOOOOOOOOOOOOOO
OOOOOOOOOOOOOOO
OOOOOOOOOOOOOOO
OOOOOOOOOOOOOOO
OOOOOOOOOOOOOOO
OOOOOOOOOOOOOOO
OOOOO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RRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK
KKKKKKKKKKK

































and it keeps going. here is input

TIGER
DOG
RACCOON
ZEBRA
AARDVARK
HIPPOPOTAMUS
CAT


if (ch!='/n')

This should be if (ch!='\n'). Use a back slash instead of a forward slash.
Topic archived. No new replies allowed.