Joao brandao Posted February 21, 2012 at 02:56 PM Report Share #440560 Posted February 21, 2012 at 02:56 PM Boas pessoal, tenho aqui uma duvida muito simples em c++, eu fiz o programa que estao a ver em baixo e gostaria de imprimir cada carácter em uma linha diferente. Em C usa se o "\n" mas em C eu sei que é a beira da declaraçao da variavel ao imprimir(um exemplo muito simples é: printf("%s\n",c) ). A minha duvida era saber como imprimir linha a linha os caracteres. Cumprimentos, Joao Brandao #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { char c[20]; int i; cin.getline(c,20); for(i=0;i<strlen(c);i++) { cout<<c[i]; } system("PAUSE"); return EXIT_SUCCESS; } Link to comment Share on other sites More sharing options...
pmg Posted February 21, 2012 at 03:12 PM Report Share #440563 Posted February 21, 2012 at 03:12 PM Imprime um '\n' 😛 Era assim que eu faria, mas nao sou um conhecedor de C++. std::cout << c[i] << '\n'; What have you tried? Não respondo a dúvidas por PM A minha bola de cristal está para compor; deve ficar pronta para a semana. Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código! Link to comment Share on other sites More sharing options...
RiKoNnEcT Posted February 21, 2012 at 03:13 PM Report Share #440564 Posted February 21, 2012 at 03:13 PM Comentei a alteração 😛 #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { char c[20]; int i; cin.getline(c,20); for(i=0;i<strlen(c);i++) { cout<<c[i]<<endl; //Basta meteres aqui o "endl" } system("PAUSE"); return EXIT_SUCCESS; } Link to comment Share on other sites More sharing options...
Joao brandao Posted February 21, 2012 at 04:34 PM Author Report Share #440575 Posted February 21, 2012 at 04:34 PM ah kk B) muito obrigada pessoal 😛 Link to comment Share on other sites More sharing options...
kurayama Posted February 21, 2012 at 11:29 PM Report Share #440635 Posted February 21, 2012 at 11:29 PM Duas pequenas correcções, tenta evitar strings de C, usa std::string em vez de char[]. E calcula o tamanho da string só uma vez, como meteste na condição do 'for' calculas o tamanho por cada iteração. Tens aqui uma versão mais C++(11) 😛 #include <string> #include <iostream> int main(int argc, char *argv[]) { std::string s; std::getline(std::cin, s); for(char c : s) std::cout << c << std::endl; return 0; } Link to comment Share on other sites More sharing options...
pikax Posted February 22, 2012 at 01:46 AM Report Share #440642 Posted February 22, 2012 at 01:46 AM Duas pequenas correcções, tenta evitar strings de C, usa std::string em vez de char[]. E calcula o tamanho da string só uma vez, como meteste na condição do 'for' calculas o tamanho por cada iteração. Tens aqui uma versão mais C++(11) 😛 #include <string> #include <iostream> int main(int argc, char *argv[]) { std::string s; std::getline(std::cin, s); for(char c : s) std::cout << c << std::endl; return 0; } que for é esse???? A solução "melhor" no standart de STD é utilizar os iteratores que seria assim: #include <string> #include <iostream> int main() { std::string s; std::getline(std::cin, s); /* for(int i=0;i < s.size();i++) std::cout << s[i] << std::endl; */ for(std::string::iterator it=s.begin();it!=s.end();++it) std::cout<< *it <<std::endl; } Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast." Link to comment Share on other sites More sharing options...
pedrosorio Posted February 22, 2012 at 02:16 AM Report Share #440644 Posted February 22, 2012 at 02:16 AM que for é esse???? http://en.wikipedia.org/wiki/C%2B%2B11#Range-based_for-loop Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
pikax Posted February 22, 2012 at 02:28 AM Report Share #440645 Posted February 22, 2012 at 02:28 AM desconhecia completamente isso, é que não está a compilar, tenho a versão 4.4.1 do MinGW e não está a compilar, até meti "-std=c++0x" mesmo assim dá erro Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast." Link to comment Share on other sites More sharing options...
pedrosorio Posted February 22, 2012 at 02:41 AM Report Share #440646 Posted February 22, 2012 at 02:41 AM Sim, podes verificar aqui as versões do gcc que suportam cada uma das novas características(o range-based for loop só está disponível a partir do 4.6): http://gcc.gnu.org/projects/cxx0x.html Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
pikax Posted February 22, 2012 at 03:03 AM Report Share #440647 Posted February 22, 2012 at 03:03 AM pois instalei o MinGW 4.6 e funciona, ignorancia minha no post anterior, sempre aprender coisas novas Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast." Link to comment Share on other sites More sharing options...
kurayama Posted February 23, 2012 at 12:54 AM Report Share #440796 Posted February 23, 2012 at 12:54 AM Por acaso não tinha testado com gcc, já troquei por clang à uns tempos. Se usares iteradores, a melhor maneira no standard (C++11 é o standard actual) é usar non-member begin e end. #include <string> #include <iostream> int main() { std::string s; std::getline(std::cin, s); for(auto it = std::begin(s); it != std::end(s); ++it) std::cout<< *it <<std::endl; } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now