webForumDet fria alternativet

Lite konstiga fel.

C/C++

4 svar · 547 visningar · startad av lalja

Medlem sedan dec. 2007467 inlägg
Frågan#1

Hallå alla. Håller på med lite basics program för att förstå c++ igen; har gjort ett litet uppehål, håller nu på med ett program jag gjorde förut.
här får ni det.

#include <iostream>
#include <string>
using namespace std;
int iVal;
int iPlus;
int iPlus2;
int iMinus;
int iMinus2;
int pSumma;
int mSumma;

int main()
{
cout <<"Hello! What do you want to count? Plus =1 or Minus=2 ? (1/2)";
	cin >> iVal;
	system ("CLS");
	if (iVal==2)
	{
	cout <<" You choiced plus. Input the first number.(1-9)\n\n";
	cin >> iPlus;
	system ("cls");
	cout <<"Input the second number.(1-9)\n\n";
	cin >> iPlus2;
	system ("cls");
	pSumma = iPlus + iPlus2;
	cout <<" Result: " << pSumma << ". Thanks for using\n\n";
	cin.get();
	return main();
    }
	else if (iVal==2) 
	{
	cout <<" You choiced minus. Input the first number.(1-9)\n\n";
	cin >> iMinus;
	system ("cls");
	cout <<" Input second number.(1-9)\n\n";
	cin >> iMinus2;
	system ("cls");
	mSumma = iMinus - iMinus2;
	cout <<"Result: " << mSumma << ". Thanks for using\n\n";
	cin.get();
	return main();
	}
	cin.get();
	return 0;
	}

Felet är då att när jag trycker "2" och kommer in i minus delen så blir det endå plus? tror ju tyvär att detta e ett enkelt fel och att jag inte kan läsa mellan raderna?
vad är fel? jag har försökt och försökt med allt men ja lyckas inte lösa det?

Medlem sedan aug. 20039 340 inlägg
#2
#include <iostream>
#include <string>
using namespace std;
int iVal;
int iPlus;
int iPlus2;
int iMinus;
int iMinus2;
int pSumma;
int mSumma;

int main()
{
  cout <<"Hello! What do you want to count? Plus =1 or Minus=2 ? (1/2)";
  cin >> iVal;
  system ("CLS");
  [B]if (iVal==1)[/B]
    {
      cout <<" You chose plus. Input the first number.(1-9)\n\n";
      cin >> iPlus;
      system ("cls");
      cout <<"Input the second number.(1-9)\n\n";
      cin >> iPlus2;
      system ("cls");
      pSumma = iPlus + iPlus2;
      cout <<" Result: " << pSumma << ". Thanks for using\n\n";
      cin.get();
      return main();
    }
  [B]else if (iVal==2) [/B]
    {
      cout <<" You chose minus. Input the first number.(1-9)\n\n";
      cin >> iMinus;
      system ("cls");
      cout <<" Input second number.(1-9)\n\n";
      cin >> iMinus2;
      system ("cls");
      mSumma = iMinus - iMinus2;
      cout <<"Result: " << mSumma << ". Thanks for using\n\n";
      cin.get();
      return main();
    }
  cin.get();
  return 0;
}

Som du gissar är felet väldigt enkelt. Båda if-satserna letade efter 2. Jag passade för övrigt på att indentera koden bättre och rätta felstavningen "choiced".

Medlem sedan dec. 2007467 inlägg
#3

Man tackar! Uppskattar till tusen, ska kolla den nu, men iaf, tack :D :D

Medlem sedan dec. 2007467 inlägg
#4

Funkade klockrent. tack ännu en gång

Medlem sedan dec. 2004736 inlägg
#5

Edit: D'oh, det var ju gjort när jag hade postat det. Ta gärna bort det här :r

260 ms totalt · 4 externa anrop · v20260731065814-full.2f471f9e
128 ms — deklarationer (db)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)
128 ms — ändringar (db)