im just starting with If, else, true statements and im stuck.

# include <iostream>
# include <cmath>
# include <iomanip>
using namespace std;

int main()
{
    int score, days;
    bool Y = true;

    cout <<"Enter an assignment score and type 'Y' if it was late: ";
    cin >> score >> Y;

    if (score > 100)
        {
            cout <<"Error: ";
        }

    if (score < 0)
        {
            cout <<"Error: ";
        }

    if (Y)
        {
            cout <<"\nHow many days was it late?: " << endl;
            cin >> days;
        }



    system("pause");
    return 0;
}

well its a succesful build but when it doesn let me type how many days it was late. It just skips to press any key to continue, am i missing something?

Recommended Answers

All 6 Replies

"cin >> Y" parses a boolean (which i think just parses a number); not a "Y"; are you confused because you have a variable named "Y"?

As mentioned by bugmenot this is how you do it.

# include <iostream>
# include <cmath>
# include <iomanip>
using namespace std;

int main()
{
	int score, days;
	char Y = 0;

	cout <<"Enter an assignment score and type 'Y' if it was late: ";
	cin >> score >> Y;

	if (score > 100)
		{
			cout <<"Error: ";
		}

	if (score < 0)
		{
			cout <<"Error: ";
		}

	if (Y = 'Y')
		{
			cout <<"\nHow many days was it late?: " << endl;
			cin >> days;
		}



	system("pause");
	return 0;
}

>>if (Y = 'Y')
Sorry, Rajith, but that's not how to do it. You need the == operator, not the assignment operator.

// ...
bool Y = true;
cout <<"Enter an assignment score and type 'Y' if it was late: ";
cin >> score >> Y;
// ...

the bool literals are true and false . when performing formatted input or output, the character sequences that bool values are translated from/to depends on the format flag std::ios::boolalpha . if set, the same literals true and false are used. if unset (this is the default), the literals used are 1 and 0 .

# include <iostream>
# include <sstream>
# include <string>

int main()
{
  std::string input = "0 false 1 true" ;
  std::istringstream stm(input) ;

  // make std::cin get it's input from the string 
  std::cin.rdbuf( stm.rdbuf() ) ; 

  bool flag ;

  std::cin >> flag ; // ios::boolalpha unset (default)
  std::cout << std::boolalpha << flag << ' '
            << std::noboolalpha << flag << '\n' ;

  std::cin >> std::boolalpha >> flag ;
  std::cout << std::boolalpha << flag << ' '
            << std::noboolalpha << flag << '\n' ;

  std::cin >> std::noboolalpha >> flag ;
  std::cout << std::boolalpha << flag << ' '
            << std::noboolalpha << flag << '\n' ;

  std::cin >> std::boolalpha >> flag ;
  std::cout << std::boolalpha << flag << ' '
            << std::noboolalpha << flag << '\n' ;

  std::cin.rdbuf(0) ; // restore default
}

alright thanks for all the help guys =D

and one more thing lets say the number enter is 85.

well since it was late i have to take off 20% and i dont know how to do that.

its very easy to remove the percentage out.

Lets consider the marks entered as 'x'

therefore we need to remove 20% from the marks entered ..............

That is 20% of X

Let "newX=20% of X"

therefore you can use this

"newX= (20/100)*X"

So we know know X and the marks that we need to take out "newX"

Hence you can do this with

("marks" = X-newX)


This is the formula point of view And i guess you will not have much problem in coding the whole thing out .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.