WolfmanZ Posted April 30, 2012 at 03:11 PM Report #452271 Posted April 30, 2012 at 03:11 PM Boas estou com uma duvida ao ler dados da consola por exemplo o utilizador introduz algo deste genero: 3 //numero de estruturas a serem criadas 2 1 3 // ler primeiro numero para um parametro da estrutura, 2º para outro parametro e 3º para outro 10 5 1 4 3 2 como faço para ler da linha de baixo do 3 e para ler os numeros separados? Obrigado Cumps
HappyHippyHippo Posted April 30, 2012 at 03:14 PM Report #452272 Posted April 30, 2012 at 03:14 PM o melhor é usar o modelo fgets/sscanf char buffer[256]; int p1, p2, p3; fgets[buffer, 256, stdin); sscanf(buffer, "%d%d%d", &p1, &p2, &p3); IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
WolfmanZ Posted April 30, 2012 at 03:21 PM Author Report #452279 Posted April 30, 2012 at 03:21 PM char buffer[256]; int p1, p2, p3; fgets[buffer, 256, stdin); sscanf(buffer, "%d%d%d", &p1, &p2, &p3); para ler com o sscanf como faço depois para passar para a estrutura visto que será uma estrutura por linha?
Localhost Posted April 30, 2012 at 03:25 PM Report #452281 Posted April 30, 2012 at 03:25 PM Primeiro lês a quantidade de estruturas i.e. linhas, depois fazes um ciclo que vai ser executado até que essa quantidade de linhas seja lida. scanf ("%d", &n); for (int i = 0; i < n; i++) scanf ("%d %d %d", &estrutura.campo1, &estrutura.campo2, &estrutura.campo3); here since 2009
WolfmanZ Posted April 30, 2012 at 03:28 PM Author Report #452283 Posted April 30, 2012 at 03:28 PM sim mas como passo imagina 1 0 3 // linha 1 para estrutura x 2 4 5 // linha 2 para estrutura y ou seja como faço tendo um array de estruturas para mudar para a estrutura seguinte?
Localhost Posted April 30, 2012 at 03:31 PM Report #452285 Posted April 30, 2012 at 03:31 PM Eu dei um exemplo, pensei que conseguirias chegar lá através dele. Nesse caso vais ter que ter, obviamente, um vector de estruturas e o scanf muda para isto: scanf ("%d %d %d", &estrutura[ i ].campo1, &estrutura[ i ].campo2, &estrutura[ i ].campo3); here since 2009
WolfmanZ Posted April 30, 2012 at 03:34 PM Author Report #452287 Posted April 30, 2012 at 03:34 PM ok obrigado pela ajuda!
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