output problem

i can't see any output why???

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
  #include<iostream>
#include<string>
using namespace std;
int find(int a[12]);
int main()
{
	int t=0,len=0,temp[12]={0} ,k=0;
	string str;
	//temp array will be the final array for difference 
	char prime[12]={'C','G','I','O','S','Y','a','e','g','k','m','q'};
	for(int j=0;j<t;j++)
	{
		cin>>len;
		getline(cin,str,'\n');
		while(k<str.length())
		{
			for(int i=0;i<12;i++)
			{
				if(str[k]>prime[i])
				{
					temp[i]=str[k]-prime[i];
				}
				else
				{
					temp[i]=prime[i]-str[k];
				}
				
				
			}
			//find the index of the smallest element in temp array to fix that index in str and going through another element
			int i=find(temp);
			str[k]=prime[i];
			k++;
		}
		cout<<str;
	}
	cin.ignore('\n');
	cin.get();
	return 0;
}
int find(int a[12])
{
	int s;
	for(int i=0;i<12;i++)
	{
		if(a[i]<a[i+1])
			s=i;
		else
			s=i+1;
	}
	return s;
}
Hi @melinda345,
I've noticed in your code this:
1
2
//line 7: t=0;
//line 11: for (int j=0; j<t; j++) 

This means that if 0<0, then execute. Since 0 is not smaller than 0, you won't get any loop executions, therefore no output.
Hope this helps.
H i@Troaat

no even when it's not zero i can't see any output
:(
Alright, I've tried a few stuff and found something:
As you've probably heard before, cin can get stuck or something.
In your case, whenever you input a new "len", getline() is blocked.
1
2
3
cin>>len;
cin.get();
getline(cin,str);

So whenever you use cin.get() or getline(), put a cin.get() before it just to make sure.

EDIT
With this you should be able to even input the numbers AND strings.
Last edited on
Oh and one more thing, in your find() function:
1
2
3
4
5
6
7
for(int i=0;i<12;i++)
{
	if(a[i]<a[i+1])
		s=i;
	else
		s=i+1;
}

Your array is 12 components long.
If you count its index 0 through 11, it's perfect.
But if 's' gets the value i+1, when it reaches component #11, s' value will be 12.
At the same time you'll have the last execution with the form if (a[11]<a[12], which cannot be.
I don't know, maybe you already knew that and intended to do that, but if not I suggest you check the components 0 through 10.
1
2
3
4
5
6
7
for(int i=0;i<11;i++)
{
	if(a[i]<a[i+1])
		s=i;
	else
		s=i+1;
}

With this it means you check the elements 2 by 2.
thank you @Troaat but
i'm so mixed up still doesn't work i've done the changes

when i want to see the out put for this input after all i hit enter and nothing apears and console screen closed
input:
1//number of test cases in this code variable t will control that
6//the len of the string(cin>>len)
AFREEN//reading the input string by getline

program desciption:
i wanna replace every charecter in input string
with the nearest prime asci code charecter

PLZ HELLLP
Last edited on
Hi, melinda345.
Could you please help me to understand what you are trying to achieve?
You do not provide any input (string str is declared, but left empty), so what’s the expected output?

I’d also point out:
line 3: --> using namespace std;
The less you use this instruction, the better.
In this case, find() already exists in std namespace. It's defined in <algorithm>.
You could simply declare:
1
2
3
using std::cin;
using std::cout;
using std::endl;


line 13 --> cin>>len;
len is filled with user input... and then neglected.
Also, it should be a good idea to add a std::cout that indicates what input your program is waiting for.

line 31 --> int i = find(temp);
It’s perfectly legal, but you are aware that you are hiding another “i” variable, are you?
It doesn’t seem to give any problem in this situation, but I’m not sure if it makes your code clearer.

line 4 --> int find(int a[12]);
When you pass a monodimensional array as the parameter of a function, you are just sending a pointer.
There’s no difference in writing int find(int a[12]) or int find(int* a), apart from the fact that the second better describe the reality. Anyway your code is legal.

Please, add some details to what you aim to do in order to make simpler to help you.

Topic archived. No new replies allowed.