neilofbodom
Member
If you have scanf before fgets then it will skip fgets.
Use fgets instead of scanf.
no scanf, just using the fgets.
If you have scanf before fgets then it will skip fgets.
Use fgets instead of scanf.
It should work, I will try personally and check the problem, basically fgets doesn't get skipped, it goes to that line of code and encounters a /n which directly returns from the function and goes to next line.no scanf, just using the fgets.
It should work, I will try personally and check the problem, basically fgets doesn't get skipped, it goes to that line of code and encounters a /n which directly returns from the function and goes to next line.
I will debug the code personally and tell you, till that time try using getchar(); before fgets and run.
EDIT:
I ran you code on my PC, works fine for me.
Just try closing your IDE and opening it again and run it.
If you just want the user to edit the file then ask him what field to edit, take the input in a variable then just paste it to the file.Awesome dude, it worked. Just had to tweak some things around. Could I ask you, how would you use the malloc? Also, how do you use dynamic memory allocation to load strings (probably as binary) into memory? I need to do this so that the user can have the option to edit an entry. I thought it would be best to load into memory, edit the entry and load it back to the text file.
Cheers!!
If you are using C++ use a String, it's just so much easier. http://www.cplusplus.com/reference/string/string/
Otherwise, using malloc can be a pain especially when using it to resize char arrays for input and you don't know how long the input might be.
If you just want the user to edit the file then ask him what field to edit, take the input in a variable then just paste it to the file.
What you can do is scan the file till the address section is reached, then the remaining characters in the file will need to be retrieved in a variable then after that you will have to append the edited field to the field, then after that append the variable data.That's what I was thinking. But how would I paste the edited field in the file? How would I delete the content of the field and paste the new data in the previous slot?
Problem is, I am using C.
To find out how long the input might be, couldn't I find the size of the string and create an array depending on that size?
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned int len_max = 128;
unsigned int current_size = 0;
char *pStr = malloc(len_max);
current_size = len_max;
printf("\nEnter a very very very long String value:");
if(pStr != NULL)
{
int c = EOF;
unsigned int i =0;
//accept user input until hit enter or end of file
while (( c = getchar() ) != '\n' && c != EOF)
{
pStr[i++]=(char)c;
//if i reached maximize size then realloc size
if(i == current_size)
{
current_size = i+len_max;
pStr = realloc(pStr, current_size);
}
}
pStr[i] = '\0';
printf("\nLong String value:%s \n\n",pStr);
//free it
free(pStr);
pStr = NULL;
}
return 0;
}
//Darren Jones
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
string word = "";
string pig1 = "";
string pig2 = "";
int novowels = 0;
cout << "Please enter a word to be translated. (-1 to end) " << endl;
getline(cin, word);
while (word != "-1")
{
for (int x = 0; x < word.length(); x += 1)
{
if (word.substr(x, 1) != "a" && word.substr(x, 1) != "e" && word.substr(x, 1) != "i" && word.substr(x, 1) != "o" && word.substr(x, 1) != "u")
{
novowels = 1;
pig2 = "way";
pig1 = word;
}
}
if (novowels = 0)
{
if (word.substr(0, 1) == "a" || word.substr(0, 1) == "e" || word.substr(0, 1) == "i" || word.substr(0, 1) == "o" || word.substr(0, 1) == "u")
{
pig2 = "way";
pig1 = word;
}
else
{
for (int x = 0; x < word.length(); x += 1)
{
while (word.substr(x,1) != "a" || word.substr(x,1) != "e" || word.substr(x,1) != "i" || word.substr(x,1) != "o" || word.substr(x,1) != "u")
{
pig2 += word.substr(x,1);
word.erase(x, 1);
pig2 = word;
}
pig2 += "ay";
}
}
}
cout << "The word is " << pig1 << "-" << pig2 << endl;
cout << "Please enter a word to be translated. (-1 to end) " << endl;
getline(cin, word);
}
system("pause");
return 0;
}
If it doesn't have any vowels at all then just add -way to the end.
The English language doesn't typically have words without vowels. Haha.
Regardless, what you want to do is search the word for a vowel, and when it finds one, go back one index and create 2 substrings, one with the letters to move, and the other with the rest of the letters.
Then, you want to concatenate the beginning letters to the end of the other letters the same way you add "way" to the end of the words.
In C you use String.h for such manipulations. I don't know if it's different in C++.
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
string word = "";
string pig1 = "";
string pig2 = "";
int vowelfound = 0;
int vowellocation = 0;
cout << "Please enter a word to be translated (-1 to enter) ";
getline(cin, word);
while (word != "-1")
{
vowellocation = 0;
pig1 = word;
pig2 = "-way";
for (int x = 0; x < word.length(); x += 1)
{
if (vowellocation == 0)
{
if (word.substr(x, 1) == "a" || word.substr(x, 1) == "e" || word.substr(x, 1) == "i" || word.substr(x, 1) == "o" || word.substr(x, 1) == "u")
{
vowellocation = x;
break;
}
}
else break;
}
if (vowellocation > 0)
{
pig2 = "-" + word.substr(0, vowellocation) + "ay";
pig1 = word.substr(vowellocation);
}
cout << "The word translated is " << pig1 << pig2 << endl;
cout << "Please enter a word to be translated (-1 to enter) ";
getline(cin, word);
}
system("pause");
return 0;
}
If first != vowel
add -way
Else
Grab letters until first vowel
Append letters to end
Add -way
//Darren Jones
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string Name = "";
string Last = "";
string InputId = "";
string Id = "";
ifstream inFile;
int main()
{
inFile.open("Advanced27.txt", ios::in);
if(inFile.is_open())
{
cout << "Enter employee code, enter -1 to end: ";
cin >> InputId;
cin.ignore();
while(InputId != "-1")
{
while(!inFile.eof())
{
getline(inFile, Id, '#');
inFile>>Name;
inFile.ignore();
inFile>>Last;
inFile.ignore();
if(Id == InputId)
{
cout << Name << " " << Last << endl;
}
}
cout << "Enter employee code, enter -1 to end: ";
cin >> InputId;
cin.ignore();
}
}
else
{
cout<<"File not found."<<endl;
}
system ("pause");
return 0;
}
You never reset the file to read from the beginning; having gone through the list once, the file reaches EOF and since it remains there, the condition of the inner loop fails every time after the first.
(Hint: use seekg)
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int hour = 0;
int minute = 0;
int desired = 0;
double angle = 0;
double time = 0;
cout << "Enter hour: ";
cin >> hour;
cout << "Enter minute: ";
cin >> minute;
cout << "Enter a desired angle: ";
cin >> desired;
angle = (hour*30 + minute*.5)- minute*6;
if (angle > 180)
{
angle = angle -360;
}
if(angle < 0)
angle = angle/-1;
angle = desired - angle;
time = angle/6 + ((minute/60));
cout << "Time in minutes: " << time << endl;
system("pause");
return 0;
}