Skip to content

Instantly share code, notes, and snippets.

@xyos
Created June 11, 2012 16:54
Show Gist options
  • Save xyos/2911224 to your computer and use it in GitHub Desktop.
Save xyos/2911224 to your computer and use it in GitHub Desktop.
agenda con numeros
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream agenda("agenda.txt");
if (agenda)
{
// contamos el numero de lineas en el archivo
int n = 0, k = 0;
string s;
while(getline(agenda,s))
{
n++;
}
// regresamos al principio del archivo para operaciones posteriores
agenda.clear();
agenda.seekg(0, ios::beg);
//numero de numeros en la agenda = lineas/3
int n_numbers = n/3;
while(true)
{
cout << "hay " << n_numbers <<
" numeros en la agenda, ingrese la posicion que quiere ver, 0 para salir \n";
cin >> n;
if(n == 0)
{
break;
}
if(n>0 && n <= n_numbers)
{
while(getline(agenda,s))
{
k++;
// imprimimos las lineas en la posicion que nos indica el usuario
if(k > ((n-1)*3)+1 && k < ((n-1)*3)+4)
{
cout << s << "\n";
}
}
// regresamos al principio del archivo para operaciones posteriores
k = 0;
agenda.clear();
agenda.seekg(0, ios::beg);
}
else
{
cout << "numero errado \n";
}
}
}
else
{
cout << "error";
}
return 0;
}
1
Juan
2856932
2
pepe
2745698
3
maria
6513221
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment