C/C++ Thread

Ankur

Active Member
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.
 
Last edited:

neilofbodom

Member
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.

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!!
 

Ankur

Active Member
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 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.
 

neilofbodom

Member
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.

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?
 

Ankur

Active Member
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?
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.
But this is what I am thinking, there are better ways I think.
Plus if you are doing this in large scale then use databases instead of files.
 

Cromewell

Administrator
Staff member
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?

There's a couple ways to work around that then. Both are essentially the same. The gist of what you do is read part of your input at a time and use realloc to extend your input variable.

One way to do it is like this (originally from http://www.mkyong.com/c/how-to-handle-unknow-size-user-input-in-c/):
Code:
#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;
}
It reads a character at a time and increases the buffer size as needed.
 

Darren

Moderator
Staff member
Alright I'm in C++ at my school and I need to finish this program. Basically I have to translate to pig latin following these rules.

If the word begins with a vowel (except y) add -way to the end of the word.

If it doesn't begin with a vowel then add a dash to the end of the word then move the first letter to the end of the word until you hit a vowel as your first letter, then add ay to the end of the word. (ex chair would be air-chay).

If it doesn't have any vowels at all then just add -way to the end.


I'm using Visual Studio 2010 by the way.

This is what I have so far and as it stands it just adds -way to the end of whatever I try. Confused mainly as to how to remove the letters and put them at the end of the word following rule 2.


Code:
//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;
}
 

Troncoso

VIP Member
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++.
 

Darren

Moderator
Staff member
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++.

Yeah I know the no vowel thing is weird but it's part of it and she'll check it. I know what I have to do and I created two strings but still no matter what I do, it always just adds -way to the end of it. Really starting to get on my nerves.
 

Darren

Moderator
Staff member
This is what I ended up with after some help from a friend and it works now. This was a complete scrap of the last one and start afresh.


Code:
#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;
}
 

Troncoso

VIP Member
Well, you have 3 cases, so what you need to do is grab the first letter:

If first != vowel
add -way
Else
Grab letters until first vowel
Append letters to end
Add -way

Even just doing it like this will cover the "no vowels" case, as the result is the same for words beginning with constants and works with no vowels.

I'm going over your code now. It's overly complicated.

EDIT: Just noticed your post. Did that fix your problem?
 

Darren

Moderator
Staff member
Yeah it did. The second one was much better. I was reading through my code and realized I could cut out almost half of it and still accomplish the same thing. I did get some help from a friend though but was able to figure it out. Thanks for the help. My issue was just confusing myself with various loops and ifs.
 

Darren

Moderator
Staff member
Back again with a presumably easy question. My loop only works the first time I run it. The program is supposed to search a sequential access file for names matching the code you entered. Everything works fine the first time I run it but every time after that it just prompts me for an employee code without displaying those that match it.

Contents of file.

F1#John James
P2#Mary Dapper
F2#Mary Jones
P1#Jeff Bride
F2#Joel Adkari
P2#Kristy Jacob
F1#June Joana
F1#Jacob Sueba
P2#Tammy Janes
F1#Charles Smith
P1#Bridget Hines
P2#Jerry Kramer
P1#John Adams
P2#William Smith
P1#Jenny Adair
F1#Connie Gray
F1#Chris Thomas
F1#Carl Angles
P2#Jenny Jonah
F1#Carol Verace
P1#Ann Jerame
P2#Kate Krumpet
F2#Janice Paulo
P1#Suman Cary
P1#Ann Patel

Code:

Code:
//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;

}
 

TrainTrackHack

VIP Member
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)
 

Darren

Moderator
Staff member
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)

I understand what you're saying but this has never been an issue before when doing similar things without using seekg (no idea what that even is actually).
 

TrainTrackHack

VIP Member
Well, then it's simply the fact that you've been doing similar things but not the exact same thing (I can't comment on exactly why your other programs have worked without seeing the code first). As for seekg, I'm assuming this is a homework problem so I'll leave it for you to look up, but you can use it to move the get pointer (the pointer used for reading the file) back to the start of the file.
 

Darren

Moderator
Staff member
Back once again. I'm doing practice problems for an upcoming programming competition and me and my friend are truly stumped.

Question:

Assume that the hands of the clock move continuously around the face. Given the hour and the minute and a desired angle in degrees (0-360) between the hands, determine the smallest time before that angle occurs between the hands. Note that hands do not always line up "on the minute". Inputs will be integer.

Examples

hour: 12
minute: 10
desired angle: 100
output in minutes : 7.4

hour: 5
minute: 5
desired angle: 30
output in minutes : 16.3

Thanks for any tips.

I'm using Visual Studio 2010.


Code:
#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;
}
 
Top