Why does it print twice the same thing?

Hi,

I haven't, am not and do not plan on learning C++, all I know is from forums or from a book I read two years ago. I am trying to make a program that calculates prices and resources needed for certain “Modules” for a MMO.

I tried making a while loop to allow multiple modules to be put, but it displays the same thing twice instead of once (can't figure out why). Here is what the interface looks like :

**IF YOU WANT TO LEAVE THE CALCULATOR, JUST CLOSE THE WINDOW**

What module do you want to install? (Examples:'Avenger' or 'VLLT')
dd // My input

DD. COST each : 60,000 cr. + 2 droids

How many do you want? (Modules left:15) **YOU CURRENTLY HAVE 0 OF THEM**
(NOTE : You may enter a negative number if you previously made a mistake)
5 // My input

-------------------------------------------------------------------------------
Current Total Module Price : 300000 cr
+10 droids
-------------------------------------------------------------------------------

What module do you want to install? (Examples:'Avenger' or 'VLLT')
// No input possible, it just prints this directly again :

-------------------------------------------------------------------------------
Current Total Module Price : 300000 cr
+10 droids
-------------------------------------------------------------------------------

What module do you want to install? (Examples:'Avenger' or 'VLLT')
// Now it lets me input something.


Here is a sample of my source code :

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
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

using namespace std;


int main()
{
	struct resources	// Defines the amount of resources and their price
	{
		int price;
		int amount;
		
			resources()
			{
				price =0;
				amount=0;
			}

	} droid;

		int no_dd=0;

	int price=0;	// Total price of Modules
	int last;	// Price of last Module
	int nom;	// Number of Modules
	int tnom=0;	// Total Number of Modules

	
	std::cout<<"**IF YOU WANT TO LEAVE THE CALCULATOR, JUST CLOSE THE WINDOW.**"<<endl;

while (true)
{
	last=NULL;
	std::cout << "\nWhat module do you want to install? (Examples:'Avenger' or 'VLLT')"<<endl;
	std::string input;
	std::getline(std::cin,input);


	transform(input.begin(), input.end(), input.begin(), ::tolower);

	// DD
	if (input == "defense drone" || input == "dd")
	{
		cout<<"\nDD. COST each : 60,000 cr. + 2 droids" <<endl;
		last  =60000;
				//-------------------------
				while (true)
				{
					std::cout<<"\nHow many do you want? (Modules left:"<<15-tnom<<")";
					std::cout<<"	      **YOU CURRENTLY HAVE "<<no_dd<<" OF THEM**";
					std::cout<<"\n(NOTE : You may enter a negative number if you previously made a mistake)\n";
					std::cin >>nom;
					

					if (tnom+nom > 15 || tnom+nom < 0)
					{
						cout<<"Sorry, that's not valid. The amount of modules must stay between 0 and 15.\nNever mind, let's try again.\n"<<endl;
						continue;
					}

					if (no_dd+nom<0)
					{
						cout<<"Nah, you can't have less than 0 DD's.\n";
						continue;
					}

					price+=nom*last;
					tnom+=nom;
					no_dd+=nom;
					droid.amount+=2*nom;
					break;
				}
				//-------------------------
	}

	// Definition of other “modules”

	cout<<"\n-------------------------------------------------------------------------------\nCurrent Total Module Price : "<<price<<" cr";

	if(droid.amount == 0) { }
	else
	{
		cout<<"\n+"<<droid.amount<<" droids";
	}

	cout<<"\n-------------------------------------------------------------------------------\n";
}
	
	return 0;

}
Last edited on
Add std::cin.ignore(); after line 55. I can see other things you probably need to change, but that should fix your current problem.
Topic archived. No new replies allowed.