My program crashs

the compiler doesnt give to me any error, but when I run my program It crash as soon as I run it,why is it happening?

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
  #include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class bench{
	private:
		vector<string> name;
		vector<int>  amount;
public:
	
		void CopyName(string boardname);
		void CopyAmount(int amountboard);
		void ShowCopyName();
		void ShowCopyAmount();
		void ShowNameAmount();
	};
	
	void bench::CopyName(string boardname){
		
			name.push_back(boardname);
	}
	
	void bench::CopyAmount(int amountboard){
			
			amount.push_back(amountboard);
	}

	void bench::ShowCopyAmount(){
		
		for(vector<string>::iterator it = name.begin();it != name.end();++it){
			cout<<*it;
			cout<<" ";
		}
	}
		void bench::ShowCopyName(){
		
		for(vector<int>::iterator it = amount.begin();it != amount.end();++it){
			cout<<*it;
			cout<<" ";
		}
	}
	
	
int main(int argc, char **argv)
{
	bench ratula;
	string word ;
	string exit (exit);
	while(word.compare(exit)){
	cout<<"Insert  board name:";
	cin>>word;
		ratula.CopyName (word);
	}
		
	int number;
	while(number != 0){
	cout<<"Insert  board amount:";
	cin>>number;
		ratula.CopyAmount (number);
	}
	
}
I believe the error is in these lines:
1
2
int number;
	while(number != 0){


You compare number to 0 even though variable number has not a value yet.
Yes,that's true, just I have to inizialitate,thanks!!
Now, the program has a weird behaviour, when I run the program It works fine for the first loop in the while, but after that It has a strange behaviour


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
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class bench{
	private:
		vector<string> name;
		vector<int>  amount;
public:
	
		void CopyName(string boardname);
		void CopyAmount(int amountboard);
		void ShowCopyName();
		void ShowCopyAmount();
		void ShowNameAmount();
	};
	
	void bench::CopyName(string boardname){
		
			name.push_back(boardname);
	}
	
	void bench::CopyAmount(int amountboard){
			
			amount.push_back(amountboard);
	}

	void bench::ShowCopyAmount(){
		
		for(vector<string>::iterator it = name.begin();it != name.end();++it){
			cout<<*it;
			cout<<" ";
		}
	}
		void bench::ShowCopyName(){
		
		for(vector<int>::iterator it = amount.begin();it != amount.end();++it){
			cout<<*it;
			cout<<" ";
		}
	}
	
	
int main(int argc, char **argv)
{
	bench ratula;
	int number = 0;
	string word ("nothing") ;
	string exit ("exit");
	while(word.compare(exit)!=0){
	cout<<"Insert  board name:";
	 getline (cin,word);
	ratula.CopyName(word);
	cout<<"Insert  board amount:";
	cin>>number;
	ratula.CopyAmount (number);
	}
		
	
}
Is the strange behaviour an endless loop that it keeps outputing insert?

@Edit
This may correct any strange behaviour you may be having

1
2
3
4
5
6
7
8
9
10
while (word.compare(exit) != 0)
{
	cout << "Insert  board name:";
	getline(cin, word);
	ratula.CopyName(word);
	cout << "Insert  board amount:";
	cin >> number;
	cin.ignore();      //added
	ratula.CopyAmount(number);
}
Last edited on
yes, It doesnt stop in getline(cin,word);I mean one it has asked in the first loop(with a normal behaviour),It does this:
Insert board name: Insert board amount:

Insert board name: Insert board amount:
Insert board name: Insert board amount:


It seems like It's skipping the getline(cin,name)
Yes,It did, but why, could you explain me why, it was happening and why doing that It has been fixed?

Thanks so much!!
 
getline(cin, word);

function getline by default reads everything until it encounters the newline character '\n' which is also extracted but discarded as well.cin will read one word leaving the newline character behind for the next call to getline to find it before you have a chance to input anything.That is the reason why it seems like it is skipping the getline while in fact it does not.It just terminates immediately because of the newline character left by cin.And that is also why the call to cin.ignore() fixes the problem.Because it clears the newline character left by cin.
ok, cool thank!!!

Other thing,more personla,ehehehehe, are you learning to code in C++??.

I would like to learn more, and I'm on that, would you be interested in doing any proyect (something modest) with me?

If you have something in mind I would like to help
I am currently studying computer science and I find C++ very interesting to work with.Unfortunately I am currently busy with other lessons as well.If you come up with an idea of a project send me a message though :)
I'm electronic engineer, I my final proyect was Espresso-II, it's an algorithm to simflicate boolean functions, and I did that in c++...so now I'm starting to remeber and studying for myself C++, It's quite different than electronic but I can find it quite interesting,ok I will if I find something interesting.
Topic archived. No new replies allowed.