//============================================================================
// Name : quiz3.cpp
// Author : Ploutarchos
// Version :
// Copyright : Your copyright notice
// Description : Quiz 3rd C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
#include <cstdlib>
usingnamespace std;
template <class T>
T power(T n, int r){
T result;
if(r==0)
return 1;
else
result=n* power(n,r-1);
return result;
}
bool convertToint(constchar *str1, int&);
int main() {
int n,r;
int size, Num;
bool IsNum ;
//question1
cout << "pls give me 2 numbers: " << endl;
cin >> n >> r;
cout<<"the result of "<<n<<" power to "<< r <<" is: " << power(n,r) << endl;
//question2
cout <<"pls give the size of the string: "<< endl;
cin>> size;
cin.ignore();
char* str1 = newchar[size+1];
cout <<"pls give the string that you want to convert: "<<endl;
cin.getline(str1,size,'\n');
IsNum=convertToint(str1, Num);
if(IsNum)
cout<<"the string that you gave me is a number: "<< Num<<endl;
else
cout<<"the string that you gave me is not a number: "<< endl;
return 0;
}
bool convertToint(constchar *str1, int&Num){
int i=0;
while(str1[i]!='\0'){
if(isdigit(str1[i])==0)
returnfalse;
i++;
}
Num= atoi(str1);
returntrue;
}
my serious problem is on bool convertToint(const char *str1, int&);
that return the values something is going wrong an I can find it
your code: if(isdigit(str1[i])==0)
is same as: if(true == false) //false
OR if(false == false)//true
this is probably not what you expected.
and will program do in this case if it's false.
--if true return false.
--if false return ture-- skip to return true.