getting error in printing

Aug 27, 2013 at 10:31pm
Write your question here.

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
  #include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
string num;
string to_num(string temp)
{
	int j;
		for (j = 0; j < 7; j++)
		{
	
			if (temp[j] == 'A' || temp[j] == 'B' || temp[j] == 'C')
				temp[j] = '2';
			if (temp[j] == 'D' || temp[j] == 'E' || temp[j] == 'F')
				temp[j] = '3';
			if (temp[j] == 'G' || temp[j] == 'H' || temp[j] == 'I')
				temp[j] = '4';
			if (temp[j] == 'J' || temp[j] == 'K' || temp[j] == 'L')
				temp[j] = '5';
			if (temp[j] == 'M' || temp[j] == 'N' || temp[j] == 'O')
				temp[j] = '6';
			if (temp[j] == 'P' || temp[j] == 'S' || temp[j] == 'R')
				temp[j] = '7';
			if (temp[j] == 'T' || temp[j] == 'U' || temp[j] == 'V')
				temp[j] = '8';
			if (temp[j] == 'W' || temp[j] == 'X' || temp[j] == 'Y')
				temp[j] = '9';
		}
		num = temp;
		return num;
}
void print(vector <string> *str,int n)
{
	int count = 1,i,j,temp[200];
	for (i = 0 ; i < n; i++)
	{
			if (str[i] == str[i + 1])
				++count;
			else
				if (count != 1)
				{
					printf ("%S%d",str[i],count);
					count = 1;
				}
	}	
}

int main()
{
	int n,i,j;
	vector <string> str;
	string temp;
//	vector <int> :: iterator pd;
	cout << "enter n" << endl;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		string num;
		cin >> temp;
		num = to_num(temp);
		str.push_back(num);
	}
	stable_sort (str.begin(), str.end());
	print(&str,n);
	for (i = 0; i < n; i++){
		cout<<str[i];
		cout <<endl;}
	return 0;
}
Last edited on Aug 27, 2013 at 10:55pm
Aug 27, 2013 at 10:38pm
Could you at least tell us what the problem is and what is supposed to happen?
Aug 27, 2013 at 10:42pm
sry 4 nt clearify.....actually i want to print the string which have more then 1 count value...but here even i cant compile my code,,its giving some error like

"48 33 C:\Users\elkana\Documents\vector.cpp [Error] cannot pass objects of non-trivially-copyable type 'class std::vector<std::basic_string<char> >' through '...'""
i.e. the problem is i passing or accepting the vector as argument....
Aug 27, 2013 at 10:49pm
Could you please copy and paste the entire error message? If it's very long use a paste service and give the link.
Aug 27, 2013 at 10:50pm
I do not know that you are trying to do but it seems the following function has a problem.

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
string to_num(string temp)
{
	int j;
		for (j = 0; j < 7; j++)
		{
			if (temp[j] == '-')
			{
				j--;
			}
			if (temp[j] == 'A' || temp[j] == 'B' || temp[j] == 'C')
				temp[j] = '2';
			if (temp[j] == 'D' || temp[j] == 'E' || temp[j] == 'F')
				temp[j] = '3';
			if (temp[j] == 'G' || temp[j] == 'H' || temp[j] == 'I')
				temp[j] = '4';
			if (temp[j] == 'J' || temp[j] == 'K' || temp[j] == 'L')
				temp[j] = '5';
			if (temp[j] == 'M' || temp[j] == 'N' || temp[j] == 'O')
				temp[j] = '6';
			if (temp[j] == 'P' || temp[j] == 'S' || temp[j] == 'R')
				temp[j] = '7';
			if (temp[j] == 'T' || temp[j] == 'U' || temp[j] == 'V')
				temp[j] = '8';
			if (temp[j] == 'W' || temp[j] == 'X' || temp[j] == 'Y')
				temp[j] = '9';
		}
		num = temp;
		return num;
}


Let assume that temp starts from "A-B"

in this case when j == 0 A is substituted to 2. When j == 1 temp[j] == '-'. You decrease j. So j again will be equal to 0. In loop the j increased and becomes equal to 1. Again temp[1] == '-'. As the result you get an infinit loop.
Aug 27, 2013 at 10:55pm
"L. B." I HAVE ALREADY COPIED PASTE THE ERROR IN ABOVE...
Aug 27, 2013 at 10:55pm
If the function accepts pointer to vector then this code

if (str[i] == str[i + 1])

is invalid

You should write

if ( (*str )[i] == (*str )[i + 1])
Aug 27, 2013 at 10:57pm
You can also use references instead of pointers. (Generally references should be taught/learned before pointers...oh well.)

greatcoder wrote:
"L. B." I HAVE ALREADY COPIED PASTE THE ERROR IN ABOVE...
That is not the complete error. Also, when you type in all caps it makes it sound like you're yelling and angry, and it also makes it hard to read.

Anyway, vlad pointed out the mistake and solution, and I gave a suggestion, so don't worry about it anymore unless you run into another problem ;)
Last edited on Aug 27, 2013 at 10:58pm
Aug 27, 2013 at 10:57pm
HEY NOW I HAVE EDIT THE ABOVE CODE BUT ITS THE SAME PROBLEM...EARLIER I ASSUME THE I/P NOT TO PUT "-".....THE PROBLEM IS IN
PRINTF ("%S%d",STR[i],COUNT);
THE MAIN PROB..IS IN STR[i].....
Aug 27, 2013 at 10:59pm
Did you follow vlad's advice?

And again, please don't type in all caps.
Last edited on Aug 27, 2013 at 11:00pm
Aug 27, 2013 at 11:00pm
no2...l b m nt angry not at all...sry 4 my ruthness......but thats the only error my compiler is showing am using devc++...
Aug 27, 2013 at 11:02pm
greatcoder wrote:
thats the only error my compiler is showing
Vlad posted how to fix it in bold.
greatcoder wrote:
am using devc++...
Make sure you're using the Orwell Dev-C++, as the original Dev-C++ is extremely outdated and behind the times in C++ support. There's an article about it:
http://www.cplusplus.com/articles/36vU7k9E/
Aug 27, 2013 at 11:03pm
I do not understand what you are trying to do in this errorneous function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void print(vector <string> *str,int n)
{
	int count = 1,i,j,temp[200];
	for (i = 0 ; i < n; i++)
	{
			if (str[i] == str[i + 1])
				++count;
			else
				if (count != 1)
				{
					printf ("%S%d",str[i],count);
					count = 1;
				}
	}	
}


Even if you will write the expression correctly

if (str[i] == str[i + 1])

as

if ( (*str)[i] == (*str)[i + 1])

there is no element of the vector with index n that is gotten when i == n-1.
Aug 27, 2013 at 11:09pm
opps ya thats corrects...it sud be "n - 1";......but still its giving me the same problem...

48 36 C:\Users\elkana\Documents\vector.cpp [Error] cannot pass objects of non-trivially-copyable type 'class std::basic_string<char>' through '...'
Aug 27, 2013 at 11:13pm
Did you also change str[i] to (*str)[i] on line 45 with the printf?

Also, printf/scanf are from C, and cout/cin are from C++. You cant mix these n your programs like this, pick either the C or the C++ and stick with it.
Aug 27, 2013 at 11:18pm
i did it......already...actually i really need "printf" here coz my main motive is to print as"3digit num -- 4 digit num" to be mention its few..
Aug 27, 2013 at 11:24pm
greatcoder wrote:
actually i really need "printf" here coz my main motive is to print as"3digit num -- 4 digit num" to be mention its few..
You can do that without printf.
Last edited on Aug 27, 2013 at 11:25pm
Aug 28, 2013 at 12:13am
hey lb , i gt the right answer after replacing printf with cout...but i dnt get the concept still nw ,why cant i use printf here,,,..
Aug 28, 2013 at 2:06am
You cannot mix cin/cout with scanf/printf - they operate on different wavelengths. You can use 100% scanf/printf or 100% cin/cout, but you can not mix them together. They both operate on your program's input and output streams, but they operate in different ways that conflict with each other.

I'm having a hard time thinking of an analogy so I hope you understand it just can't be done.
Topic archived. No new replies allowed.