Hi, I'm trying to make this code know if the value I'm entering for n is a
letter and give me an error whenever I do so, but I'm having trouble with it.
I'm sure I have to convert the n to a char, but I'm not sure what to do since all I can think of is doing ch=static_cast<char>(n);.
Whenever I do that either the menu shows up, but doesn't let me do anything else or it says n wasn't initialized, depending on where I put it.
Edit:Didn't add the rest of the code because I didn't think it was necessary...
double n, boot total=0;
char select, ch;
bool flag=false;
do
{
cout<<"\t\t\tMENU PRINCIPAL"<<endl;
do
{
cout<<"-------------"<<endl;
cout<<"-------------"<<endl;
cout<<"Select a case: ";
cin>>select;
switch(select)
{
case'1':
if (flag == false)
{
system("cls");
cout<<"You're in Case #1..."<<endl;
cout<<"Enter N before proceeding = ";
cin>>n;
boot=n;
cout<<"Continue by pressing Enter"<<endl;
system("pause");
flag=true;
}
elseif(n >= 'A' && n <= 'Z')
{
cout<<"You should only enter a numeric value for n"<<endl;
cout<<"Enter another value for n."<<endl;
cin>>n;
flag=false;
}
else(n >= 'a' && n <= 'z')
{
cout<<"You should only enter a numeric value for n"<<endl;
cout<<"Entre el valor de n de nuevo."<<endl;
cin>>n;
flag=false;
}
system("pause");
}
n=boot;
break;
case'2':
if (flag == true)
{
cout<<"Serie #1 (OPCION 2)"<<endl;
for (n; n<=100; n++)
{
total +=(pow(-1.0, n-1))*(((2*n)+(1))/(n*(n+1)));
cout<<"n = "<<n<<" sumatoria= "<<total<<endl;
}
cout<<"Result of Serie #1 = "<<total<<endl;
cout<<"Continue by pressing Enter"<<endl;
system("pause");
}
else
{
system("cls");
cout<<"Go to Option #1 1st!!! "<<endl;
system("pause");
}
n=boot;
total=0;
break;
}
}while(select != '1' && select != '7');
}while(select == 1);
I know I'm not doing anything with the ch in that code.
Any ideas?
why the heck are you trying to make a double into a char? chars are ints. There is no char for 2.5 only for 2 and 3. Also singed chars are -127 - 128 or signed chars from 0 - 255. Also doubles are not precise 4 could really be 3.9999999999999 or 4.000000000000000000001 so just change from double to int. Also, you do not need to do static cast try this
In this case i would be n right? Since this is an assignment I have to specifically enter the value of n in the cmd. What I'm trying to do is, whenever I enter a letter from a to z it gives me:
1 2 3
cout<<"You should only enter a numeric value for n"<<endl;
cout<<"Enter another value for n."<<endl;
So if I put a numeric value to n, i this case 60, I wouldn't be testing anything...
Just edited the code. Sorry I didn't add it sooner, I didn't think it was necessary but I put it now just to know exactly what we're dealing with.
Made the n an int but i have this in another case:
total += (1)/((n*(sqrt(n+1.0)))+(n+1)*(sqrt(static_cast<double>(n))));
Did what you told me but I get this output:
1 2 3 4 5 6 7 8 9 10 11 12 13
Estas en la Opcion #1...
Enter N before proceeding = b
Continue by pressing Enter
Press any key to continue . . .
MENU PRINCIPAL
1.) Validate n
2.) Serie # 1
3.) Serie # 2
4.) Serie # 3
5.) Serie # 4
6.) Serie # 5
7.) Exit
-------------
Select case: Press any key to continue . . .
Where it says "Select case" that's where I'm supposed to enter the number of the case i want to switch to but instead I get that output...