Case statement Weirdin' out on me

char?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2060 Views - Last Post: 14 May 2001 - 12:41 PM Rate Topic: -----

#1 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4503
  • View blog
  • Posts: 28,411
  • Joined: 21-March 01

Case statement Weirdin' out on me

Posted 09 May 2001 - 06:09 PM

Allright, heres some code, I dont know why this is happening, but when I type in yes for the case statement it won't accept it and scrolls thru the code really fast. I dont know why this is, and i'm guessing most of this code doesnt matter, but I put it in anyways just in case. Thanks in advance


#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <fstream.h>
#include <iomanip.h>
#include <string.h>
#include <ctype.h>
void head_title_name();//head_title_name function
void read_input();//read_input function
void dayenter();//calc_info function
void tempcalc();//results function
void do_again();//do_again function
int i,j,k,m;//declare as variables
int temp[10][3];//multi dimensional array
int num;
char y_n;//declare as variable
ofstream outfile;//enables outfile
ofstream infile;//enables infile
setw();//enables setw
void main()//main function
{
head_title_name();
}//end function
void head_title_name()
{
cout << "C++ Organizer: Josh Rivera 'aka' sloth
";
cout << "Class: Computer Science, RRHS
";//introduces program
cout << "Instructor: Mr H, Block 4
";
cout << "Program #6: Celsius-Farenheit Conversion
";
do_again();
}//end function
void do_again()
{
cout << "
Would you like to enter 7 day values?
";//prompts user
cout << "Press Y for Yes or N for No and press enter to continue
";
cin  >> y_n;
system("cls");
switch (y_n)
{case 'No':
case 'NO':
case 'no'://no answer
case 'N'://capital N anser
case 'n'://lower case n answer
system("cls");
cout << "Thank You
";
break;
case 'YES':
case 'Yes':
case 'yes'://yes answer
case 'Y'://capital Y answer
case 'y'://lowercase y answer
cout << "Thank You for playing
";
dayenter();
break;
default:
cout << "Please read and follow all directions
";//if directions aren't followed
do_again();
break;}
}//end function
void dayenter()
{
outfile.open("outprogram3", ios::out);//opens outfile
//enter:
cout << "Please enter the value for day #1   ";
    cin >> temp[i][0]; 
cout << "Please enter the value for day #2   ";
cin>> temp[i][1];
cout << "Please enter the value for day #3   ";
cin>> temp[i][2];
cout << "Please enter the value for day #4   ";
cin>> temp[i][3];
cout << "Please enter the value for day #5   ";
cin>> temp[i][4];
cout << "Please enter the value for day #6   ";
cin>> temp[i][5];
cout << "Please enter the value for day #7   ";
cin>> temp[i][6];

//if ((tri[i][0]<=0) || (tri[i][1]<=0) || (tri[i][2]<=0)) //{cout << "Cannot have a negative measurement or a measurement equal to 0 ";//no 0's or negative numbers //goto enter; system("cls");//clears screen cout << "  Day #         Celsius Reading        Farenheit Reading ";//begin chart cout << "  -----         ---------------        ----------------- "; outfile << "  Day #         Celsius Reading        Farenheit Reading "; outfile << "  -----         ---------------        ----------------- "; tempcalc();//calls tri_measure }//endfunction void tempcalc() { cout << "Day #1:"<<temp[i][0]<<setw(16)<<(((temp[i][0]-32)/9)*5)<<setw(25)<<(((temp[i][0]*9)/5)+32)<<' '; outfile << "Day #1:"<<temp[i][0]<<setw(16)<<(((temp[i][0]-32)/9)*5)<<setw(10)<<(((temp[i][0]*9)/5)+32)<<' '; cout << "Day #2:"<<temp[i][1]<<setw(16)<<(((temp[i][1]-32)/9)*5)<<setw(25)<<(((temp[i][1]*9)/5)+32)<<' '; outfile << "Day #2:"<<temp[i][1]<<setw(16)<<(((temp[i][1]-32)/9)*5)<<setw(10)<<(((temp[i][1]*9)/5)+32)<<' '; cout << "Day #3:"<<temp[i][2]<<setw(16)<<(((temp[i][2]-32)/9)*5)<<setw(25)<<(((temp[i][2]*9)/5)+32)<<' '; outfile << "Day #3:"<<temp[i][2]<<setw(16)<<(((temp[i][2]-32)/9)*5)<<setw(10)<<(((temp[i][2]*9)/5)+32)<<' '; cout << "Day #4:"<<temp[i][3]<<setw(16)<<(((temp[i][3]-32)/9)*5)<<setw(25)<<(((temp[i][3]*9)/5)+32)<<' '; outfile << "Day #4:"<<temp[i][3]<<setw(16)<<(((temp[i][3]-32)/9)*5)<<setw(10)<<(((temp[i][3]*9)/5)+32)<<' '; cout << "Day #5:"<<temp[i][4]<<setw(16)<<(((temp[i][4]-32)/9)*5)<<setw(25)<<(((temp[i][4]*9)/5)+32)<<' '; outfile << "Day #5:"<<temp[i][4]<<setw(16)<<(((temp[i][4]-32)/9)*5)<<setw(10)<<(((temp[i][4]*9)/5)+32)<<' '; cout << "Day #6:"<<temp[i][5]<<setw(16)<<(((temp[i][5]-32)/9)*5)<<setw(25)<<(((temp[i][5]*9)/5)+32)<<' '; outfile << "Day #6:"<<temp[i][5]<<setw(16)<<(((temp[i][5]-32)/9)*5)<<setw(10)<<(((temp[i][5]*9)/5)+32)<<' '; cout << "Day #7:"<<temp[i][6]<<setw(16)<<(((temp[i][6]-32)/9)*5)<<setw(25)<<(((temp[i][6]*9)/5)+32)<<' '; outfile << "Day #7:"<<temp[i][6]<<setw(16)<<(((temp[i][6]-32)/9)*5)<<setw(10)<<(((temp[i][6]*9)/5)+32)<<' ';

cout << "The average Celsius reading for this 7 day period is: "<<((((temp[i][0]-32)/9)*5)+(((temp[i][1]-32)/9)*5)+(((temp[i][2]-32)/9)*5)+(((temp[i][3]-32)/9)*5)+(((temp[i][4]-32)/9)*5)+(((temp[i][5]-32)/9)*5)+(((temp[i][6]-32)/9)*5))/7<<' '; cout << "The average Farenheit reading for this 7 day period is: "<<((((temp[i][0]*9)/5)+32)+(((temp[i][1]*9)/5)+32)+(((temp[i][2]*9)/5)+32)+(((temp[i][3]*9)/5)+32)+(((temp[i][4]*9)/5)+32)+(((temp[i][5]*9)/5)+32)+(((temp[i][6]*9)/5)+32))/7<<' '; outfile << "The average Celsius reading for this 7 day period is: "<<((((temp[i][0]-32)/9)*5)+(((temp[i][1]-32)/9)*5)+(((temp[i][2]-32)/9)*5)+(((temp[i][3]-32)/9)*5)+(((temp[i][4]-32)/9)*5)+(((temp[i][5]-32)/9)*5)+(((temp[i][6]-32)/9)*5))/7<<' '; outfile << "The average Farenheit reading for this 7 day period is: "<<((((temp[i][0]*9)/5)+32)+(((temp[i][1]*9)/5)+32)+(((temp[i][2]*9)/5)+32)+(((temp[i][3]*9)/5)+32)+(((temp[i][4]*9)/5)+32)+(((temp[i][5]*9)/5)+32)+(((temp[i][6]*9)/5)+32))/7<<' ';

outfile.close();//closes outfile cout << " ";//newline do_again();//calls do_again }//end function--end program


(Edited by supersloth at 3:04 pm on May 14, 2001)

(Edited by supersloth at 3:07 pm on May 14, 2001)


Is This A Good Question/Topic? 0
  • +

Replies To: Case statement Weirdin' out on me

#2 tsmoke  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 20-April 01

Re: Case statement Weirdin' out on me

Posted 09 May 2001 - 06:42 PM

Quote

switch (y_n)
{case 'No':
case 'NO':
case 'no'://no answer
case 'N'://capital N anser
case 'n'://lower case n answer
system("cls";
cout << "Thank You
";
break;
case 'YES':
case 'Yes':
case 'yes'://yes answer
case 'Y'://capital Y answer
case 'y'://lowercase y answer
cout << "Thank You for playing
";
dayenter();
break;
default:
cout << "Please read and follow all directions
";//if directions aren't followed
do_again();
break;}

There are several things that you might want to do first to make this code a little more clearer. First off the variable y_n is of type char. Since you show that a person might enter more than Y or N. you might want to turn that into a string type.

I haven't looked too close at the other code, but when data is enter by the user, it goes into the stream. since you are pulling out data of type character, when you type more than just one character the rest of the characters are left in the stream.

So first change y_n to type string, then before you execute the case statement, get just the first letter of the string and change the case to either upper or lower. Then all you have to do is make the case statement on two values. Check out the following:

char answer = toupper(y_n[0]);

switch (answer)
{
  case "N": system("cls";
            cout << "Thank You
";
            break;
  case "Y": cout << "Thank You for playing
";
            dayenter();
            break;
  default: cout << "Please read and follow all directions
";//if directions aren't followed
           do_again();
           break;
  }

See how you do with that.

Was This Post Helpful? 0
  • +
  • -

#3 runtime error  Icon User is offline

  • Lucky.Code
  • member icon

Reputation: 3
  • View blog
  • Posts: 629
  • Joined: 19-March 01

Re: Case statement Weirdin' out on me

Posted 09 May 2001 - 07:15 PM

many many people were asking me this question while they were doing thier final.

You can't use a switch statement with a character array. instead try doing this:

if (strcmp(y_n,"yes"==0));

and include <string.h>

i am not sure on the context, check in your book just in case.

Was This Post Helpful? 0
  • +
  • -

#4 nighthawk  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 1,269
  • Joined: 11-April 01

Re: Case statement Weirdin' out on me

Posted 10 May 2001 - 10:35 AM

ok sloth, i don't mean to rip this apart but i can see a few little details that can be fixed...
1) you tell the user to input y for yes or n for no, keep that variable as a char and do a switch for y and n (as runtime said, you can only do switches on chars or ints or bools)
2)you don't ask the user what they're inputing the temperature as (i.e. f or c) you need to know this to convert it.
3) once you know f or c, i would do a switch for them and put all of your equations into that switch statement.
4) instead of doing this:

Quote

cout << "Day #1:"<<temp[0]<<setw(16)<<(((temp[0]-32)/9)*5)<<setw(25)<<(((temp[0]*9)/5)+32)<<'
';
outfile << "Day #1:"<<temp[0]<<setw(16)<<(((temp[0]-32)/9)*5)<<setw(10)<<(((temp[0]*9)/5)+32)<<'
';
cout << "Day #2:"<<temp[1]<<setw(16)<<(((temp[1]-32)/9)*5)<<setw(25)<<(((temp[1]*9)/5)+32)<<'
';
outfile << "Day #2:"<<temp[1]<<setw(16)<<(((temp[1]-32)/9)*5)<<setw(10)<<(((temp[1]*9)/5)+32)<<'
';
cout << "Day #3:"<<temp[2]<<setw(16)<<(((temp[2]-32)/9)*5)<<setw(25)<<(((temp[2]*9)/5)+32)<<'
';
outfile << "Day #3:"<<temp[2]<<setw(16)<<(((temp[2]-32)/9)*5)<<setw(10)<<(((temp[2]*9)/5)+32)<<'
';
cout << "Day #4:"<<temp[3]<<setw(16)<<(((temp[3]-32)/9)*5)<<setw(25)<<(((temp[3]*9)/5)+32)<<'
';
outfile << "Day #4:"<<temp[3]<<setw(16)<<(((temp[3]-32)/9)*5)<<setw(10)<<(((temp[3]*9)/5)+32)<<'
';
cout << "Day #5:"<<temp[4]<<setw(16)<<(((temp[4]-32)/9)*5)<<setw(25)<<(((temp[4]*9)/5)+32)<<'
';
outfile << "Day #5:"<<temp[4]<<setw(16)<<(((temp[4]-32)/9)*5)<<setw(10)<<(((temp[4]*9)/5)+32)<<'
';
cout << "Day #6:"<<temp[5]<<setw(16)<<(((temp[5]-32)/9)*5)<<setw(25)<<(((temp[5]*9)/5)+32)<<'
';
outfile << "Day #6:"<<temp[5]<<setw(16)<<(((temp[5]-32)/9)*5)<<setw(10)<<(((temp[5]*9)/5)+32)<<'
';
cout << "Day #7:"<<temp[6]<<setw(16)<<(((temp[6]-32)/9)*5)<<setw(25)<<(((temp[6]*9)/5)+32)<<'
';
outfile << "Day #7:"<<temp[6]<<setw(16)<<(((temp[6]-32)/9)*5)<<setw(10)<<(((temp[6]*9)/5)+32)<<'
';

i'd write a for loop and put i where your number is, that makes for more precise programming.
ie:  
for(i=0; i<7; i++)
{
cout << "Day #1:"<<temp[i]<<setw(16)<<(((temp[i]-32)/9)*5)<<setw(25)<<(((temp[0]*9)/5)+32)<<'
';
outfile << "Day #1:"<<temp[i]<<setw(16)<<(((temp[i]-32)/9)*5)<<setw(10)<<(((temp[0]*9)/5)+32)<<'
';
}

5) of course, there is a problem with this, you don't know what you're converting to, so when you ask if they want f or c i'd have a f and c array that you read the numbers into, then in the switch statement of f or c i'd do a for loop just like the one above that would fill the other array with it's values.
ie:  let's say i'm putting in f values, i enter them all and you put them in the f[6] array, once all values have been entered you call a switch statement
switch f_or_c()
case 'f': //since i entered values in f this is where you'd put you equation to convert to c
for(i=0; i<7; i++) {
c[i]=f[i] (the rest of your equation)

case'c': //you’d do the same for c
(i think this step relates to 3, but hey you get the point)

i hope this helps you, if you have anymore questions, please ask.

Was This Post Helpful? 0
  • +
  • -

#5 nighthawk  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 1,269
  • Joined: 11-April 01

Re: Case statement Weirdin' out on me

Posted 10 May 2001 - 10:39 AM

oh, for your average, i'd do it like so:

int averagef=0;
int averagec=0;
int totalf=0;
int totalc= 0;

for (int i=0; i<7; i++)
{
totalf = totalf + f[i];
totalc = totalc = c[i];
}

averagef  = totalf/7;
averagec = totalc/7;

Was This Post Helpful? 0
  • +
  • -

#6 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4503
  • View blog
  • Posts: 28,411
  • Joined: 21-March 01

Re: Case statement Weirdin' out on me

Posted 10 May 2001 - 02:11 PM

thanks for all that stuff, that helps. but for some of your stuff that nighthawk said:
1) you tell the user to input y for yes or n for no, keep that variable as a char and do a switch for y and n (as runtime said, you can only do switches on chars or ints or bools) my teacher tells us that we need to cover all options. make sure that y,n,no,yes,Yes,No,YES,NO, etc. are all taken care of(you know, the in case the user screws up)
2)you don't ask the user what they're inputing the temperature as (i.e. f or c) you need to know this to convert it. he dindnt tell me either. thats all i know, and that im writing the code for it. i would have rather had it so i could have asked what they wanted, f or c.
3) once you know f or c, i would do a switch for them and put all of your equations into that switch statement. like i said earlier...
4) instead of doing this:i think i will
Was This Post Helpful? 0
  • +
  • -

#7 nighthawk  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 1,269
  • Joined: 11-April 01

Re: Case statement Weirdin' out on me

Posted 10 May 2001 - 08:16 PM

hey, i don't mean to intrude, but you can't convert a temp unless  you know what it's being given in.  go the extra step and ask, what's he going to do, mark you off for it?
Was This Post Helpful? 0
  • +
  • -

#8 runtime error  Icon User is offline

  • Lucky.Code
  • member icon

Reputation: 3
  • View blog
  • Posts: 629
  • Joined: 19-March 01

Re: Case statement Weirdin' out on me

Posted 10 May 2001 - 11:17 PM

with mr hanosh you never know.
Was This Post Helpful? 0
  • +
  • -

#9 kalpeshmjoshi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 25-April 01

Re: Case statement Weirdin' out on me

Posted 11 May 2001 - 05:30 AM

use
char y_n[4];
in switch statement
switch(y_n[0])

Now your code will not roll over, and your requirenment is also fulfill by enterring the Yes,yes,No,no,Y,y,N,n....
Now other parts of your programm i found that the enough discussion is going on about it so i think i should not comment on it

Was This Post Helpful? 0
  • +
  • -

#10 kalpeshmjoshi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 25-April 01

Re: Case statement Weirdin' out on me

Posted 11 May 2001 - 05:34 AM

use
char y_n[4];
in switch statement
switch(y_n[0])

Now your code will not roll over, and your requirenment is also fulfill by enterring the Yes,yes,No,no,Y,y,N,n....
Now other parts of your programm i found that the enough discussion is going on about it so i think i should not comment on it

Was This Post Helpful? 0
  • +
  • -

#11 nighthawk  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 1,269
  • Joined: 11-April 01

Re: Case statement Weirdin' out on me

Posted 11 May 2001 - 07:52 AM

from what i'm hearing, [edited by supersloth] surely you'll agree with me runtime, you can't convert a temp unless you know what you're converting from and too.

(Edited by supersloth at 2:10 pm on May 11, 2001)

Was This Post Helpful? 0
  • +
  • -

#12 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4503
  • View blog
  • Posts: 28,411
  • Joined: 21-March 01

Re: Case statement Weirdin' out on me

Posted 11 May 2001 - 01:13 PM

for(i=0; i<7; i++)
{
cout << " Day #"<<i+1<<":"<<temp[i][0]<<setw(16)<<(((temp[i][0]-32.0)/9.0)*5.0)<<setw(25)<<(((temp[i][0]*9.0)/5.0)+32.0)<<'
';
outfile << " Day #"<<i+1<<":"<<temp[i][0]<<setw(16)<<(((temp[i][0]-32.0)/9.0)*5.0)<<setw(25)<<(((temp[i][0]*9.0)/5.0)+32.0)<<'
';
}
does anyone know how to make the for loop run on multi-dimensional arrys. so it will switch from

temp[i][0]   temp[i][1]   temp[i][2]   temp[i][3]
temp[i][4]   temp[i][5]   temp[i][6]
and then stop

Was This Post Helpful? 0
  • +
  • -

#13 runtime error  Icon User is offline

  • Lucky.Code
  • member icon

Reputation: 3
  • View blog
  • Posts: 629
  • Joined: 19-March 01

Re: Case statement Weirdin' out on me

Posted 11 May 2001 - 03:48 PM

i agree with you, but i will not say anything at this time i will post on the 20th.
Was This Post Helpful? 0
  • +
  • -

#14 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4503
  • View blog
  • Posts: 28,411
  • Joined: 21-March 01

Re: Case statement Weirdin' out on me

Posted 11 May 2001 - 03:55 PM

that is after graduation, *hint, hint, wink, wink*
Was This Post Helpful? 0
  • +
  • -

#15 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Case statement Weirdin' out on me

Posted 11 May 2001 - 06:24 PM

Runtime, Final grades are due on the 14th, and the descision is made the 15th if you are to graduate or not. So don't worry about anything after those dates

I will be visiting Mr. Honosh on Monday during 4th period, he seams like a very interesting person and I would like to discuss why the f*** he's giving you guys a hard time. And that's totally not cool for him to threaten someone or their professors. Hanosh if you're reading this, you won't win this one, I guarantee it!

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2