function and for loop!!!

I have been trying to get this program to work for a long time please assist me, I have really tried everything i know how to do and can comprehend from google >.<
I am trying to allow the user to enter a number after hitting 'n' that counts the characters next to each of the listed letters I could only get it to work for box[1] on line 108 please help me get it to work on the others.... :/

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
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <string.h>

using namespace std;

void printWordLengths(char inputa[], char inputb[], char inputc[], char inputd[], char inpute[], char inputf[]);
void printNeighbors(int x);
char path;
int count = 0;

int main(){
	cout << "N[eighbors] W[ords] Q[quit]\n";
	
	
	while( path!='q' || path!='Q'){
		cout << "\nEnter your choice -> ";
	    cin >> path;
		
		if( path=='w' || path=='W'){
		
			char wordt[] = "These";			
			char wordc[] = "classes";			
			char worda[] = "are";			
			char wordr[] = "really,";			
			char wordrr[] = "really";
			char worde[] = "easy!";
			printWordLengths(wordt, wordc, worda, wordr, wordrr, worde);
			//cout << "W works";
			//string words[7] = {"These","classes", "are", "really,", "really", "easy!"};
			//printWordLengths(words);
		}
		if(path=='n' || path=='N')
			
			cout << "n works";
			cout << "Enter the number of letters to show ";
			cin >> count;
			printNeighbors(count);
			
	}
		
		
	
	return 0;
	
	
	}
//cout << "N[eighbors] W[ords] Q[quit]\n";
//cout << "Enter your choice -> ";
//cin >> ;
	void printWordLengths(char inputa[], char inputb[], char inputc[], char inputd[], char inpute[], char inputf[]){
	
		cout << "Word #1 is " << strlen(inputa)<< " characters.\n";
		cout << "Word #2 is " << strlen(inputb)<< " characters.\n";
		cout << "Word #3 is " << strlen(inputc)<< " characters.\n";
		cout << "Word #4 is " << strlen(inputd)<< " characters.\n";
		cout << "Word #5 is " << strlen(inpute)<< " characters.\n";
		cout << "Word #6 is " << strlen(inputf)<< " characters.\n";
	
		return;
	}
void printNeighbors(int x){

	string box [30];
	
	char e = 'e';
	char f = 'f';
	char g = 'g';
	char h = 'h';
	char i = 'i';
	char j = 'j';
	e = e - x;
		f = f - x;
		g = g - x;
			h = i - x;
			i = i - x;
			j = j - x;

	

	for(int i=0; i<x; i++)
		{
			
			box[1] = box[1] + e++;
			box[2] = box[2] + f++;
			//f
			box[3] = box[3] + f++;
			box[4] = box[4] + g++;
			
			box[5] = box[5] + g++;
			box[6] = box[6] + h++;
			
			
			box[7] = box[7] + h++;
			//box[8] = box[8] + i++;
			
			box[9] = box[9] + j++;
			box[10] = box[10] + g++;
			
			box[11] = box[11] + h++;
			box[12] = box[12] + g++;
			
		}


	cout << "The " << x << " neighbors of 'e' are:" << box[1] << " and " << box[2] << endl;
	cout << "The " << x << " neighbors of 'f' are:" << box[3] << " and " << box[4] << endl;
	cout << "The " << x << " neighbors of 'g' are:" << box[5] << " and " << box[6] << endl;
	cout << "The " << x << " neighbors of 'h' are:" << box[7] << " and " << box[8] << endl;
	cout << "The " << x << " neighbors of 'i' are:" << box[9] << " and " << box[10] << endl;
	cout << "The " << x << " neighbors of 'j' are:" << box[11] << " and " << box[12] << endl;
	return;

}
//cin.ignore();
//cin.get();
	
	

//}   
oh yes.. you forgot to initialize your "box"..
so in "box[1] = box[1] + e++;"
box[1] is undeclared, you are adding "undeclared value + e++" which displays random stuff
Last edited on
Oh thank you timmy that makes sense I understand, what you are saying, but I do not understand how to implement it, I tried to initialize it on line 66, but it didn't work, how would I go about doing it? After I initialize it will work properly?
in any case you can't '+' with a string and a char.. declare box as char box[30]..
I really don't understand what your function is doing.. can you give an example of "cout" if x is 5?
for example
(assuming they choose n)
Enter the number of letters to show-> 3 // (assuming user enters 3)

The 3 neighbors of 'e' are: bcd and fgh.
The 3 neighbors of 'f' are: cde and ghi.
ect....

Enter the number of letters to show -> 2 //(assuming user enters 2...)

The 2 neighbors of 'e' are: cd and fg.
The 2 neighbors of 'f' are: de and gh.
Last edited on
in that case you don't need box :)
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
void printNeighbors(int x){


for (char e='e'; e!='j'; e++) {
	cout << "The " << x << " neighbors of "<<e<< " are:" 
        char lowerBorder=e-x;

        //this should display bcd for e='e'
	for(int i=0; i<x; i++)
		{
			cout<<lowerBorder;
                        lowerBorder++;
		}

        //this should display fgh
        char upperBorder=e+1;
	for(int i=0; i<x; i++)
		{
			cout<<upperBorder;
                        upperBorder++;
                }

// no return in a void function
}
}
Last edited on
Thank you so much Timmy!!! To add extra characters i just declare more variables like char lowerborder2 ext..? In other words for f would i use the same code or how..? Would I implement the next character to j? I am not looking for spoonfeeding your feedback is much appreciated, thank you so much Timmy!
no problem :)
extra characters already been taken care of : look at the first 'for'

for (char e='e'; e!='j'; e++) {

it means e = 'e' then e = 'f' ,'g', 'h' stops at 'j'
you only need this code and it should cout for x=3
The 3 neighbours of e are : bcdfgh
The 3 neighbours of f are : cdeghj
etc, tiil 'j' not included.

also, in my code you can add
cout << " and "; before 'char upperborder=e+1' so that it couts for example 'bcd and fgh' and not ('bcdfgh')
Last edited on
timmy you're awesome thanks man!
Topic archived. No new replies allowed.