free radical 0 Newbie Poster

Also, how would I format them to output horizontal in a row. right now they are straight up and down.

free radical 0 Newbie Poster

Haha, thanks for the helps guys. Newbie mistake. :).

free radical 0 Newbie Poster

Hello all,

I am trying to make a lottery number generator. So I need 6 random numbers.

The current code below displays the same number 6 times. Thoughts?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


char userchoice;
int main()
{
	cout<<"Welcome to The Lottery Wizard!"<<endl<<endl;
	cout<<"This program will allow you to play the lottery from"<<endl;
	cout<<"the comfort of your computer desk."<<endl<<endl;

	

   cout  << "Do you want to play the Lottery? (y = yes, n = no):  ";

            cin.sync();
            cin.get(userchoice);
            userchoice = tolower(userchoice);  //convert user's answer to lowercase
                                                                             

            while (userchoice != 'y' && userchoice != 'n')

            {

                        cout << "Invalid response.  Enter a y for yes or n for no: ";

                        cin.sync();          // clear out buffer
                        cin.clear();          // set read to good
			            cin.get(userchoice);

            }

 system("cls");

 

 while (tolower(userchoice) == 'y')
  {           

cout<<"The six random lottery numbers are:";

int number_back;

srand( time (NULL) ); //gets seed from clock

number_back = rand() % 53 + 1; //formula for 1-53 random number




for (int count = 0; count < 6 ; count++)

cout<<number_back<<endl;

cout  << "Do you want to play the Lottery? (y = yes, n = no):  ";

            cin.sync();
            cin.get(userchoice);
            userchoice = tolower(userchoice);  //convert user's answer to lowercase
                                                                             

            while (userchoice != 'y' && userchoice != 'n')

            {

                        cout << "Invalid response.  Enter a y for yes or n for no: ";

                        cin.sync();          // clear out buffer
                        cin.clear();          // set read to good
			            cin.get(userchoice);

            }

 }


system("cls");



cout<<"Thanks for playing"<<endl<<endl;

return 0;
}
free radical 0 Newbie Poster

I figured it out:

cout  << "Do you want to play? (y = yes, n = no):  ";

            cin.sync();

            cin.get(userchoice);

 

            userchoice = tolower(userchoice);  //convert user's answer to lowercase

                                                                                

            while (userchoice != 'y' && userchoice != 'n')

            {

                        cout << "Invalid response.  Enter a y for yes or n for no: ";

                        cin.sync();          // clear out buffer

                        cin.clear();          // set for a good read

                        cin.get(userchoice);

            }

 

            if (userchoice == 'n')

                        return 0;
free radical 0 Newbie Poster

Anyone know how to help plz?

free radical 0 Newbie Poster
1.
      if ( option == 'y' )
   2.
      // what would i put here for continue?
   3.
      else if ( option == 'n' )
   4.
      exit(0); //that is correct right?
   5.
      else
   6.
      cout<<"You have entered an invalid choice, please try again";
       cin>>option;

I just dont know what to put for continue, am I going the right way here?

free radical 0 Newbie Poster

So is there a way to do it to do this with making 'y' the only way to continue the program and 'n' to exit. Not y and other chars to continue and anything but y exit.

Not sure how to do that with if statements.

free radical 0 Newbie Poster

I figured this out, what worked for me anyway:

while (!cin || user_number < 1 || user_number > 100) //makes sure no non-integers are entered

      {

            cout << "Invalid value entered.  Please enter an integer between 1 and 100: ";

            cin.clear();   //reset the read to good

            cin.sync();       //clear out the contents of the input buffer

            cin >> user_number;

      }
free radical 0 Newbie Poster

Hello,

I need to prompt the user if they want to actually execute the program or not.

Like, "Do you want to run the program, y=yes and n=no"

Then user enters y or n and the program either runs or quits.

I was thinking that would be like:

char answer;

cout<<"Do you want to run the program, y=yes and n=no";
cin>>answer;

if
(answer=y)
//I don't know what to put here to execute the program

else
//Also don't know what to put here to quit
free radical 0 Newbie Poster

Yeah I'm not sure. Giving up on verifying input, just dont enter the wrong stuff in the program.

free radical 0 Newbie Poster
# include <string>
# include <iostream>
# include <cstdlib>

using namespace std;

int main()

{
	string name;
	string test1;
	double score1;
	string test2;
	double score2;
	string test3;
	double score3;


	cout << "Welcome to the Grade Calculator Machine.  You will enter three test scores." <<endl;
	cout << "The higher of the first two test grades will be added with the third test grade" <<endl;
	cout << "to determine the final grade average for the course." <<endl<<endl;

	cout <<"Enter the student's name:";
	getline(cin, name);

	cout <<"Please enter test score 1:";
	cin >> test1;
	score1 = strtod (test1.c_str (), NULL);
  if (score1 > 0)
        cout << "Score is " << score1 << endl;
    else if (score1 < 0)
        cout << "Invalid.  You entered a negative number." << endl;
    else
        cout << "You entered either 0 or an invalid number." << endl;

	cout <<"Please enter test score 2:";
	cin >> test2;
		score2 = strtod (test2.c_str (), NULL);
  if (score2 > 0)
        cout << "Score is " << score2 << endl;
    else if (score2 < 0)
        cout << "Invalid.  You entered a negative number." << endl;
    else
        cout << "You entered either 0 or an invalid number." << endl;

	cout <<"Please enter test score 3:";
	cin >> test3;
		score3 = strtod (test3.c_str (), NULL);
  if (score3 > 0)
        cout << "Score is " << score3 << endl;
    else if (score3 < 0)
        cout << "Invalid.  You entered a negative number." << endl;
    else
        cout << "You entered either 0 or an invalid number." << …
free radical 0 Newbie Poster
string test1;
	double score1;

cout <<"Please enter test score 1:";
	cin >> test1;
	score1 = strtod (test1.c_str (), NULL);
  if (score1 > 0)
        cout << "Score is " << score1 << endl;
    else if (score1 < 0)
        cout << "Invalid.  You entered a negative number." << endl;
    else
        cout << "You entered either 0 or an invalid number." << endl;

I get a really long error message when I try this. Starts off like this...really really long:
error C2676: binary '/' : 'std::basic_string<_Elem,_Traits,_Ax>' does not define this operator or a conversion to a type acceptable to the predefined operator
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>c:\users\austin\documents\visual studio 2005\projects\scores\scores\scoresc.cpp(62) : error C2676: binary '/' : 'std::basic_string<_Elem,_Traits,_Ax>' does not define this operator or a conversion to a type acceptable to the predefined operator
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>c:\users\austin\documents\visual studio 2005\projects\scores\scores\scoresc.cpp(66) : error C2784: 'bool std::operator >=(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const _Elem *' from 'int'
1> c:\program files\microsoft visual studio 8\vc\include\string(241) : see declaration of 'std::operator >='
1>c:\users\austin\documents\visual studio 2005\projects\scores\scores\scoresc.cpp(66) : error C2784: 'bool std::operator >=(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>

free radical 0 Newbie Poster

You have to read the documentation, try out the samples, and experiment. Pay attention to the function prototypes:

http://www.cplusplus.com/reference/string/string/c_str.html
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html
http://www.cplusplus.com/reference/clibrary/cstdlib/atof.html

I linked strtod in the previous post:

atoi returns an integer. If you want a float or a double, you need to use something else.

int atoi ( const char * str );

atof returns a double, so that's better. it's basically the same as strtod, but strtod is considered "safer", I think. You can use either, but I think strtod is generally considered better.

double atof ( const char * str );

None of these functions takes a string as an argument. They take C-Strings (char*), so you need something to convert them, which is where the string::c_str function comes in (see link above):

const char* c_str ( ) const;

Ok so I would want

string test1;
const char* c_str (test1 ) const;

I really need someone to spell out an example similiar to what I'm doing, sorry this stuff just goes right over my head.

My program works fine and I was really proud, but this verifying input has got me down in the dumps.

free radical 0 Newbie Poster

Should I be using getline instead of cin?

free radical 0 Newbie Poster

Ok so I've been working on this and still cant really get it figured out.

Does atoi convert it to a number?

Here is what i was trying:

string test1;

cout<<"enter test 1:";
	cin>>test1;
if (isdigit(str[0]))
  {
test1= atoi (str)
}
test11 = strtod (test1);

I'm lost as you can see....

Not sure if I should call the test score variables string or double?

And I'm not sure if I need to use another variable instead of just test1 with strtod thats why I was using test11...I'm not much of a progammer :(.

free radical 0 Newbie Poster

You're welcome. I just noticed this though.

if
((((test1 + test3)/2)*2) >= 90)

Looks like you are dividing by 2, then multiplying by 2, which cancel each other out, so do you want to simplify it to this?

if
(test1 + test3 >= 90)

Hehe yeah I noticed that as well. I originally intended the program to take an average of the two numbers before I realized I only had test scores that were out of 50 points :).

I've been reading around on input verification and have only come across one way that may work. It created a 2 character array to enter the numbers and then converts the character to # and if that fails it prompts the users to enter only numbers.

Just wondering if that's the easiest way to approach this?

free radical 0 Newbie Poster

Wow thanks! I really wanted to create an "in between" variable to store the score, just didn't know what it was called. I will try this later today but it looks like it will solve a lot of my problems.

Thanks!

free radical 0 Newbie Poster

Hello all.

I am working on a little program here and need some help. The input is 3 test grades that are out of 50 points total, the program takes the higher of the first 2 test grades and adds that to the last test grade to determine a final score.

This is where I need my first bit of help, as you can see the code below isn't quite up to par. It will correctly give you a final grade, but it also gives 2 other grades in addition because of how I set up the if statements. I'm not sure where to add an else because I don't want it to say if you dont get an A then else you get BCDF or something. I was trying to use the && or || possibly but can't figure out how to get what I want.

Also I need help verifying the input the user enters, if they enter negative numbers or letters it crashes the program. Like something that prompts the user "no negative numbers, try again".

# include <string>
# include <iostream>

using namespace std;

int main()

{
	string name;
	float test1;
	float test2;
	float test3;

	cout << "Welcome to the Grade Calculator Machine.  You will enter three test scores." <<endl;
	cout << "The higher of the first two test grades will be added with the third test grade" <<endl;
	cout << "to determine the final grade average for the course." <<endl<<endl;

	cout …