Calling function keeps crashing !

This is my homework for Accelerated C++ 3-3
~Count how many times each word appears after inputting a tons of strings using cin.
the code inside RandC.cpp works fine if it is inside main();

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
#include <vector>
#include <string>
#include "RandC.h"

using std:: vector;
using std:: string;
using std:: cin;

int main()
{
    vector<string> Cstr;
    vector<int> Cint;
    string x ;

    RandC( Cstr ,Cint );  
}


Okay ,thanks for replying
compiled successful ,no error when compiling
But when I input aa ,then a , it began to crash....

Windows report:
Problem Event Name: APPCRASH
Application Name: Chapter_Four.exe
Application Version: 0.0.0.0
Application Timestamp: 56a9cb3c
Last edited on
RandC.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef RANDC_H_INCLUDED
#define RANDC_H_INCLUDED


#include <vector>
#include <iostream>

using std::istream ;
using std::vector ;
using std::string;

typedef vector<string> v_str ;
typedef vector<int> v_int ;

void RandC(v_str& ,v_int& ) ;  //deleted the 3rd parameter : string ; thanks @dhayden

#endif // RANDC_H_INCLUDED
Last edited on
RanC.cpp

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

#include <iostream>
#include <string>
#include <vector>

using std::cin ;
using std::endl ;
using std::string ;
using std::vector ;
using std::istream ;
using std::cout ;

typedef vector<string> v_str ;
typedef vector<int> v_int ;
typedef vector<string>::size_type v_str_t ;
void RandC(v_str& uniq ,v_int& Count)  // deleted istream ,we can use cin locally ,deleleted stirng ,locally as well 
{
    while(cin >> x)
    {
        if(uniq.empty())
            {
               uniq.push_back(x);
               Count.push_back(1);           // 1. I forgot to add one element to Count ,when empty 
             }

       bool t = false ;     
        vector<int>::size_type i = 0 ;
        while(i < uniq.size())
        {
            if(x == uniq[i])
            {
                Count[i]++;
                t = true ;
                break ;
            }

            i++;
        }

        if(t == false)
        {
            uniq.push_back(x);
            Count.push_back(1);
        }

    }

//***************************************printing !!!
  for(vector<int>::size_type i; i < uniq.size(); i++)
     {
        cout << "The numbers of the string :" << uniq[i]  <<" is :"  << Count[i] << endl;
     }


}

Last edited on
This project contains total 3 files ,
RandC.cpp , RanC.h // works as a function prototype ,RandC(....) function

main.cpp // just call the RandC() function
Hi. For future notice, you can just edit your own post by clicking the edit button, no need to spam your own thread.

It would also help to know where it crashes, learn how to debug so you can find the line of code(s) where things go wrong. But I assume the problem can be solved by initializing the string in main.cpp.
1
2
3
vector<string> Cstr;
vector<int> Cint;
string x = " "; // Initialized to a whitespace 

The problem is on line 29 in RanC.cpp. You pass Count as an empty vector (from main()). So as soon as line 29 is reached your program will crash.

I suggest that you don't use these typedefs everywhere. It is just confusing.
It appears that x doesn't need to be a parameter. It could be a local variable.

You'll find that that the code slows down when using very large input files. Look at std::map for a more efficient way to do this problem.
@coder777 Thanks ,that's the problem . I add comments on where the error occurred .
Yeah .the empty vector made my program crash !
@dhayden ,I will check that ,thanks
Topic archived. No new replies allowed.