0

I have got stuck in a strange closing of my small program!

The program calculates the amount of years to reach a saving goal. But its no problem in the code and I dont see the problem at all with my eyes.

When I run the program it stops strangely after all is done, this also when I have put cin.get(); which should make the program wait for a keypress.

No errors is detected. To the note is also added, that I have tried same code both in VS 2015 and Netbeans with same result. I made a new project in VS and copied the code from netbeans.

Hope for fast answer!

This is my code!

/* Savingscalculation.cpp : Defines the entry point for the console 
  application.*/
#include "stdafx.h"
#include "iostream"

using namespace std;

int main()
{
// Sätter variabler för programmet. 
float inputsaving;
float savingoal;
int years = 0;
float interest;
float savingsum;
float savsum;

/* Sätter ett introduktionsmeddelande, som får programmet att upplevas mer   
   proffessionellt */         
cout << " ------------------------------" << endl;
cout << "|  Räkna ut hur många år ditt  |" << endl;
cout << "|    sparande kommer att ta!   |" << endl;
cout << "|      Tryck enter för att     |" << endl;
cout << "|          fortsätta.          |" << endl;
cout << " ------------------------------" << endl;
cin.get();

cout << "---------------------------------" << endl;
cout << "Ange ett sparmål: "; // Användaren ombeds att ange ett sparmål
cin >> savingoal;           // Det angivna sparmålet sätts till variabeln     

cout << "---------------------------------" << endl;
cout << "Ange en årlig insättning: "; 
// Användaren ombeds att ange en årlig    insättning
cin >> inputsaving;                   
// Den angivna insättningen sätts till variabeln inputsaving  

cout << "---------------------------------" << endl;
cout << "Ange en årlig sparränta: "; 
// Användaren ombeds att ange en procentsats
cin >> interest;        // Den angivna räntan sätts till variabeln interest
cout << "---------------------------------" << endl;

// Inmatning klar

// uträkning börjar //

// Algoritmen för att räkna ut en insättning + räntan för ett år
savingsum = inputsaving + inputsaving*(interest / 100); cout << endl;
savsum = savingsum; 
/* Den uträknade summan kopieras in i variabeln  
savsum för vidare uträkning i loopen */


while (savingsum < savingoal) 
/* Jämför sparandet per år med sparmålet, 
loopen fortsätter tills sparmålet     uppnåtts. */
{
    years++;        // Räknar ut antal år med +1 för varje varv i loopen
    savingsum = savingsum + savsum; /* Lägger på 1 års insättning inkl.   
                                      räntan för varje nytt år (varv) */
}

// uträkning klar //

// Sparmålet uppnått och resultatet skrivs ut på skärmen. 
cout << "------------------------------------" << endl;
cout << "Du har uppnått ditt mål efter " << years << " år" << endl; 
// Skriver ut antal år tills sparmålet uppnåtts
cout << "Ditt saldo är då " << savingsum << "kr" << endl;  
// Skriver ut    saldot som är när sparmålet är nått
cout << "------------------------------------" << endl;

cin.get();
return 0;
}

Regards Henrik

2 Answers2

0

When you use cin >> interest you record the '\n' you enter when you hit enter, which is then read on the next character for cin.get(). Try calling cin.get() a second time and it should work.

Max Weinzierl
  • 1,116
  • 8
  • 13
  • I solved it! I added 3 cin.ignore(); 1) Is after "interest" input, 2) is at the first calculation, before the loop 3) is in the end of the program. Thanks for all help about my problem! – Henrik Granström Mar 05 '17 at 11:11
-1

There is some whitespace(' ', '\n' etc.) in your buffer because after cin, the newline character gets into the input buffer. You can remove it by using cin.ignore() after cin >> interest or before cin.get().

XZ6H
  • 1,712
  • 18
  • 25