Converting intergers to character
Dec 14, 2015 at 4:36am UTC
Enter numbers: 0 1 2 3 4 5 6 7 8
output:
A B C
D E F
G H I
how can i convert it using pointers
this is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
#include <limits>
using namespace std;
bool encountered[9] = {};
int main(){
int z[3][3];
char a=0,b=1,c=2,d=3,e=4,f=5,g=6,h=7,i=8,j=9;
for (int i = 0; i <3; i++)
{
for (int j=0 ; j<3 ; ++j)
{
cout << "Num " << (j+1)+3*i << ": " ;
cin >> z[i][j];
if (cin.fail())
{
std::cerr << "Invalid input" << std::endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
--j;
continue ;
}
if (z[i][j] > 9 || z[i][j] < 0)
{
std::cerr << "Invalid number, should be between 0 and 9" << std::endl;
--j;
}
else if (encountered [z[i][j]-1])
{
std::cerr << "You already entered that number" << std::endl;
--j;
}
else
{
encountered[z[i][j]-1] = true ;
}
}
}
for (char i = 0; i <3; i++)
{
for (char j=0 ; j<3 ; ++j)
{
cout << z[i][j] << " " ;
}
cout << endl<<"\n" ;
}
cout<<"Its a magic square" ;
}
Dec 14, 2015 at 6:28am UTC
ASCII to dec values.
http://www.asciitable.com/
You'll have to do a bit of simple arithmetic to get the letter you want. But it's just assigning the number 0 to 'A' and adding numbers to your character
1 2
char begin = 'a' ;
std::cout << (begin+1); // this gets you 'b';
Dec 14, 2015 at 7:51am UTC
I need a pointer one pls help
Dec 14, 2015 at 8:58am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <algorithm>
int main() {
std::string line;
std::cout << "Enter numbers: " ;
std::getline(std::cin, line);
std::istringstream iss(line);
std::for_each(std::istream_iterator<int >(iss),
std::istream_iterator<int >(),
[](int x) { std::cout << static_cast <char >('A' + x) << ' ' ; });
return 0;
}
Topic archived. No new replies allowed.